Java Reference
In-Depth Information
11. A. Tough question! The code actually compiles fi ne, so C, D, and E are incorrect. A
private method cannot be overridden, so doSomething in B is not overriding doSomething
in A. The method call to doSomething on line 8 is referring to the private method on line
2, and that is also the method that gets invoked at runtime because it is not overridden.
Therefore, the output is A and the correct answer is A.
12. E. The code does not compile, so A and B are incorrect. The parent class X declares a
final method named doSomething, and the child class Y attempts to override it on
line 13. Because a final method cannot be overridden, a compiler error occurs on line 13
and the answer is E.
13. A, B, and C. The object on the right-hand side of the equation on line 5 is a Cat object, so
the reference on the left-hand side needs to be compatible with Cat. Because Cat extends
Pet, Pet is valid, so A is correct. Because Pet implements Runnable and Cat extends Pet,
Runnable is valid so B is correct. C is correct because a Cat reference can certainly point
to a Cat object. D is incorrect; Dog is not compatible with Cat. E is incorrect; Thread is not
related to the Cat class in any way.
14. A and B. The make fi eld in Vehicle is public, so it is accessible anywhere. Therefore, A is
a correct answer. The model fi eld is protected, so it is accessible in child classes. Because
Car extends Vehicle, B is a correct answer. The year fi eld is private in Vehicle, so C
does not compile. The mileage fi eld has the default access, but Car is in a different package
than Vehicle, so D is incorrect.
15. E. The code compiles fi le, so D is incorrect. However, a ClassCastException is thrown at
runtime on line 4 when the reference b, which points to a Firefox object, is cast to an IE
reference. Therefore, the answer is E.
16. B. The ref reference points to an IE object, so the comparison line 5 is false. However,
the comparison line 7 is true because an IE object is a Browser object, so line 8 displays
Browser. Therefore, the answer is B.
17. A, B, and C. The parameter of setBrowser is Browser, so any Browser object of child of
Browser can be passed into setBrowser. Therefore, A, B, and C are correct. D is incorrect
because Object is not a child of Browser. (Browser is a child of Object, but that relation-
ship is in the wrong direction for setBrowser to be invoked successfully.) Similarly, since
String is not a child of Browser, E is also incorrect.
18. B and C. The tales reference is a Stack of FairyTale objects, which can include any child
classes of FairyTale. Putting a SnowWhite object on the stack and a FairyTale object are both
valid, so B and C are correct. Because Cinderella does not extend FairyTale, A is incorrect.
19. D. The code does not compile, so A is incorrect. The problem with the code is that
NonFictionBook extends Book but does not override read. The read method
in NonFictionBook is an overloaded version of read, so NonFictionBook must be declared
abstract. Because it is not declared abstract, the compiler points to line 10 as the cause of
the error, so the answer is D.
20. E. The code does not compile, so A and B are incorrect. Lines 7 and 8 are valid,
so C and D are incorrect. The problem with this code is the Topic declares the read method
as final, and the child class NonFictionBook attempts to override it. The compiler error is
on line 13, so the answer is E.
Search WWH ::




Custom Search