These questions are from
Marcus Green's Mock Exam
Question 42)
Given the following declaration
Integer i=new Integer(99);
How can you now set the value of i to 10?
1) i=10;
2) i.setValue(10);
3) i.parseInt(10);
4) none of the above
Answer to Question 42)
Objective 4.6)
4) none of the above
The wrapper classes are immutable. Once the value has been set it cannot be changed. A common use of the wrapper classes is to take advantage of their static methods such as Integer.parseInt(String s) that will returns an integer if the the value has been set it cannot be changed. A common use of the wrapper classes is to take advantage of their static methods such as Integer.parseInt(String s) that will returns an integer if the String contains one.
I actually have no doubts here. I just couldn't get the idea of wrappers being immutable yet (I'm having some confusions).
Question 46)
Which of the following statements are true?
1) A method cannot be overloaded to be less public in a child class
2) To be overridden a method only needs the same name and parameter types
3) To be overridden a method must have the same name, parameter and return types
4) An overridden method must have the same name, parameter names and parameter types
Answer to Question 46)
Objective 6.2)
3) To be overriden a method must have the same name, parameter and return types
Option 1 is a sneaky one in that it should read overriden not overloaded. An overriden method must also have the same return type. Parameter names are purely a programmer convenience and are not a factor in either overloading and overriding. Parameter order is a factor however.
My answer here is 2. Because as of 1.5, return type can change to a subclass of the original method's return type (covariant returns). Is this correct?
Question 56)
Given the following class
public class ZeroPrint{
public static void main(String argv[]){
int i =0;
//Here
}
}
Which of the following lines if placed after the comment //Here will print out 0.
1) System.out.println(i++);
2) System.out.println(i+'0');
3) System.out.println(i);
4) System.out.println(i--);
Answer to Question 56)
Objective 5.1)
1) System.out.println(i++);
3) System.out.println(i);
4) System.out.println(i==);
The options for this question might look suspiciously easy if you are not aware of the effects of the post-increment operators. The ++ and == operations for examples 1 and 4 only come into effect after the output operations, ie after whatever else is done to them on that line of code. Option 2 should be fairly obvious as you should know that the single quote characters indicate a char value, ie storing the character rather than the numberical value for 0.
My answer is 1 and 3. Is this a typo error? Because option 4 differs in the Question and the Answer page.
I hope you can help me clarify these. THank you!