Java Reference
In-Depth Information
19. A and C. A has the same signature and return type, and C has the same signature and
a covariant return type, so A and C are valid overriding declarations. B and E are valid
methods for a child class, but they are examples of method overloading, not overriding.
D has an incompatible return type. F is a weaker access than public, which is not allowed.
20. E. The Child class gets the default constructor because it does not defi ne a constructor
explicitly. The default constructor contains the line super(); which does not compile
because Parent does not have a no-argument constructor. Therefore, the correct answer is E.
21. A. All the code compiles fi ne, so C, D, and E are incorrect. The name method of an enum
element returns its unqualifi ed name, which for the one reference is PLUM. The color fi eld
for PLUM is purple, so the output is a PLUM is purple. Therefore, the answer is A.
22. D. The code does not compile, so A and B are incorrect. Line 7 uses the proper syntax for
an inner class accessing a fi eld in the enclosing class, so C is incorrect. Line 12 is fi ne, so E
is incorrect. On line 9, the local inner class Inner is attempting to access a non-fi nal local
variable, which generates a compiler error. Therefore, the answer is D.
23. A. The code compiles fi ne, so D and E are incorrect. The child class is overloading print,
not overriding it. The method call on line 16 invokes print in the child, and the method
call on line 17 invokes print in the parent, so the output is ChildParent. Therefore, the
answer is A.
24. C. The SpellCheck interface compiles fi ne, so A is false. B is false; a class that implements
Readable can be declared abstract and not override read. C is a true statement; a class
that implements SpellCheck must either override both checkSpelling and read or declare
itself as abstract. Because C is true, D must be false. E is false; an interface can actually
extend multiple interfaces. Therefore, the only answer is C.
25. C. Executing new Cat() means the Cat class must be loaded fi rst by the class loader,
which causes its static initializer on line 18 to execute fi rst, displaying E. The Pet instance
initializers are next, in the order they appear, so A and C are displayed. Then the Pet
constructor is invoked, displaying B, and fi nally the Cat constructor is invoked, displaying D.
The output is EACBD, so the answer is C.
Search WWH ::




Custom Search