Java Reference
In-Depth Information
8. C. The code compiles and runs fi ne, so B, D, and E are incorrect. Line 14 does cause a
“Note” from the compiler about using “unchecked or unsafe operations,” but it is not
a compiler error. The compiler has to deduce that “T” is of type String for the Hello
object on line 14, which it successfully does. Line 9 compiles fi ne because all objects have a
toString method. Lines 13 and 14 print “hi” and “there” respectively, so the output of
the code is “hithere” and the answer is C.
9. A and C. The code compiles fi ne, so A is correct and D and E are incorrect. B is not correct
because a Set does not guarantee any specifi c iteration order, and the iteration order can
change over time. Therefore, C is correct and the answers are A and C.
10. B. To fi nd the ceiling of “On“, you need to know the natural ordering of the four String
objects, which is “ONE“, “On“, “One“,and then “one“. The ceiling method returns the
least String in tree greater than “On“, which is “One“. Therefore, the answer is B.
11. E. This is a trick question! Each of the answers attempts to “add” a key-value pair to the
map, but the Map interface does not declare an add method. Instead, you “put” elements in
a map using the put method, so none of the statements are valid and the answer is E. (By
the way, if you change add to put, then A, B, and C would be correct and D would generate
a compiler error because 'x' is not a String.)
12. A. The code compiles fi ne, so E is incorrect. The values array is sorted using the
MyComparator class, which sorts strings in alphabetical order because it ignores uppercase
characters. The order of values alphabetically is aab, Abb, and abc, so the answer is A.
13. D. The code compiles fi ne, so A, B, and C are incorrect. The for loop puts 10 pairs in the
map. The keys range from 1 to 10 and they map to their squared value. For example, 1
maps to 1, 2 maps to 4, 3 maps to 9, 4 maps to 16, and so on. Line 7 prints out the value
whose key is 4, which is 16. Therefore, the answer is D.
14. C. The code compiles and runs fi ne, but an array must be sorted before invoking the
Arrays.binarySearch method. Therefore, the result is undefi ned and the answer is C.
15. A, B, and D. The generic for the printException method must be an Exception or a child
of Exception. A is valid because FileNotFoundException is a child of Exception. B is
valid because the generic is Exception. C and E are not valid because Throwable is not a
child class of Exception. D is valid and demonstrates the syntax for specifying the type
explicitly when invoking a generic method. Therefore, A, B, and D are the correct answers.
16. B, C, and D. The showSize method can accept a List object with any generic type. A is
not a valid statement (regardless of the showSize method) because when you instantiate a
generic type, you cannot use a wildcard in the new statement. You must declare a specifi c
data type. E is not a valid statement either (regardless of the showSize method);
<IOException> and <Exception> are not compatible. The other three statements are valid
List declarations, and because any generic List can be passed into showSize because of
the wildcard <?>, the answers are B, C, and D.
17. D. The code compiles and runs fi ne, so A, B, and C are incorrect. The asList method creates
a fi xed-size list with four Integer objects. Line 4 sorts the numbers into their natural order.
Line 5 converts the list to an array using the generic toArray method of List. Line 6 prints
out the fi rst element in the array, which is the smallest value: -1. Therefore, the answer is D.
Search WWH ::




Custom Search