Java is the future !: Quiz of the Day | November 1, 2010

Monday, November 1, 2010

Quiz of the Day | November 1, 2010


Which of the following statements about the following code fragment are true?

a.) The code compiles successfully and displays "Test".
b.) Changing the code at line 13 to "public void print(int x)" will allow the code to compile correctly.
c.) The code fails to compile. The compiler complains about duplicate methods.
d.) Changing the return type on line 13 from 'void' to 'int' will allow the code to compile correctly.


Solution : Choices B and C are the correct answers.

The code appears to be attempting to overload the print() method.

However, the only differentiating feature between the methods is the static keyword. This is insufficient for overloading. Therefore, choice C is correct and choice A is incorrect because the code fails to compile due to duplicate methods.

Choice B is correct because the methods are different overloaded versions of the print() methods.

Choice D is incorrect because the return parameters are not sufficient to differentiate between the overloaded methods.

More about overloading is available at http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.9


Source : "Facebook community"

No comments:

Post a Comment