Java Reference
In-Depth Information
18. B. The code compiles fi ne, so E is incorrect. The array is sorted in reverse alphabetical
order, so the order of the sorted list is “bd“, “ba“, “ab“, then “aa“. The index of “ab” is 2,
which is displayed on line 19. Therefore, the answer is B.
19. C. The code does not compile. An array is converted to a list using the static Arrays.
asList method. Line 7 attempts to invoke asList as if it is a method of the array object,
which it is not. Line 7 generates a compiler error and therefore the answer is C.
20. D. The names list is sorted on line 6, so its order is “Dick“, “Harry“, “Sue“, and then
“Tom“. The binarySearch call on line 6 assigns x to the index of “Tom“, which is 3.
Therefore, the answer is D.
21. B. The code compiles fi ne, so D and E are incorrect. Line 14 is false because one and two
do not point to the same object. Line 16 is true; the equals method of List returns true if
and only if both lists have the same size, and all corresponding pairs of elements in the two
lists are equal. Because one and two are both of size 1 and contain the same String object
“abc“, they are equal and line 17 prints out B. Therefore, the answer is B.
22. B. The code compiles fi ne, so D and E are incorrect. The only difference between Question
21 and Question 22 is line 12: this time the two reference points to a Vector instead of an
ArrayList. Line 14 is false because one and two do not point to the same object. Line 16
is actually true; the equals method of List does not make a distinction about the actual
List implementation, so an ArrayList can equal a Vector as long they have the same size
and all corresponding pairs of elements in the two lists are equal. Because one and two are
both of size 1 and contain the same String object “abc“, they are equal and line 17 prints
out B. Therefore, the answer is B.
Search WWH ::




Custom Search