Java Reference
In-Depth Information
9. B. The argument on line 22 is a short. It can be promoted to an int, so print on line 5
is invoked. The argument on line 23 is a long. It can be promoted to a float, so print on
line 8 is invoked. The argument on line 24 is a double. It can be promoted to a java.lang.
Double, so print on line 11 is invoked. Therefore, the output is intfloatObject and the
correct answer is B.
10. C. This is a tricky question. The code does not compile, so A and B are incorrect. All fi elds
in an interface are implicitly fi nal, and static fi nal fi elds must be initialized. Line 3 compiles
fi ne, as do lines 6 and 7, so D, E, and F are incorrect. Because MAX_TRACE is not initialized,
line 2 generates a compiler error. Therefore, the answer is C.
11. A, C, and E. The urls parameter is variable length, so any number of Strings can be
passed in after the int argument. A has no Strings, C has one String, and E has three
Strings, so these answers are correct. B does not pass in the required int and generates a
compiler error. D passes in three ints, which also generates a compiler error.
12. D. The class does not compile, so A and B are incorrect. Line 16 compiles and is the proper
syntax for instantiating a new Inner object outside of the Outer class, so C is incorrect. An
inner class cannot declare static fi elds or methods, so line 5 generates a compiler error and
the answer is D.
13. B. The code compiles fi ne, so D and E are incorrect. The printResults method in Child
is overloading printResults in Parent, not overriding. In method overloading, the return
type can be any data type, so printResults in Child returning an int is not a problem.
Invoking printResults with an int argument calls the method on line 8, which displays
In Child. Therefore, the answer is B.
14. B. The ordinal method of an enum element returns its corresponding int value. Enums
are zero-based, so VANILLA is 0, CHOCOLATE is 1, and STRAWBERRY is 2. Therefore, the
answer is B.
15. E. The return type of an overridden method must either be the same or a child class of the
return type of the parent method. Because double is not a child class of float (they are
primitive types), line 8 generates a compiler error. Therefore, the answer is E.
16. A. The code compiles fi ne. A class can contain a static nested class, so B is incorrect. Line
13 can access only fi elds of Box which it does, so C is incorrect. Shipment can use the Box
class without any special syntax or prefi xes, so line 17 is valid and D is incorrect. Shipment
has access to the private methods of Box, so line 20 is valid and E is incorrect. The volume
of the Box is 10*10*10 = 1000, so the output is 1000 and the answer is A.
17. E. A case statement on an enum data type must be the unqualifi ed name of an
enumeration constant. You cannot use their ordinal values in a case. Therefore, a compiler
error occurs on lines 11, 13, and 15, so the answer is E.
18. B and E. The code compiles fi ne, so A is incorrect. B is a true statement. C is incorrect
because inner classes have access to all private fi elds of the enclosing class. D
is incorrect because the scope of a method declaration is really not relevant. (The method can
still be invoked at any time.) E is a true statement. Therefore, the correct answers are B and E.
Search WWH ::




Custom Search