|
SCJP 5.0 Mock Exam (Answers) - 1
1)Ans : 1
compile time error
equals(java.lang.Object) in Employee cannot override equals(java.lang.Object) in java.lang.Object; attempting to assign weaker access privileges; was public
equals method in Employee Class should be public boolean equals(Object x)
2)Ans : 3
The code will compile fine as well as run
since equals method in class Employee is present It will check whether two isntance of Employee are equal or not and return true or false
In this case e1 and e3 are equal
Option 5 is incorrect because for legal override of equal method of Object class
Syntax should be public boolean equals(Object o) instead of public boolean equals(Employee o)
For overriding argument must be same.
3)Ans : 2
class ABC
{
transient int x=9;
int hashCode()
{
return x;
}
}
(for eg consider value of variable x as 9. x is transient if x is serialized then after deserialization value of x is 0 due to which same object of ABC may get different hashcode value)
transient variable should not be used in hashCode method definiton
During Deserialization transient variable get a default value
4)Ans : 1
Iterator next() method will return Object.
It can't be implicitly converted to Employee
As a result compiler error
5)Ans : 4
The code will compile as well as run
Since HashSet Collections is not a ordered and not a sorted Collections
It doesn't guaranteed how element will be placed
Therefore output is not predictable
6)Ans : 1
Compile Time Error
Map interface doesnot extend the Collection interface
iterator() method is defined in Collection interface
Map interface doesnot have iterator() method
Therefore above code will give compile time error as cannot find symbol->iterator();
7)Ans : 4
The code will compile as well as run.
Employee class override the equals and hashCode method of Object class
Therefore x1 and x3 are are equal
Only unique key are allowed in map
Size will be 2
8)Ans : 1
Arrays class has a method
static boolean equals(Object[], Object[])
static boolean equals(primitive[], primitive[])
But in our case x is Object[] and x1 is primitive[]
Arrays class doesnot have a method taking both object[] and primtive[]
Therefore it will give compile time error
9)Ans is 1,2,6
option 1 will compile since the notation List<?> the wildcard <?> mean it can take any generic type
option 2 will compile since non-generic type can be assign to generic type
option 3 will not compile since generic type are not same(<Employee> and <DeptManager>
option 4 will not compile since ArrayList<DeptManager>; is missing round bracket .
It should be List<? extends Employee> x3 = new ArrayList<DeptManager>();
option 5 will not compile since generic type are different
List<? extends ProjManager> mean List can accept ProjManager and its subtpe
since DeptManager is not subtype of ProjManager it will give compile time error
option 6 will compile fine
List<? super ProjManager> mean List can accept ProjManager and it supertype
Employee is Supertype of ProjManager .List<? super ProjManager> x5 = new ArrayList<Employee>(); will work
10)Ans : 2
runtime error
If the declared array type is a class, you can put objects of any subclass of the
declared type into the array. Hence code will compile
It will give runtime error-> java.lang.ArrayStoreException: Employee at x[0] = new Employee();
x can hold only DeptManager and it subtype
|