Page: 12
12. Collections and Generics
55. Given:
import java.util.*;
public class Group extends HashSet<Person> {
public static void main(String[] args) {
Group g = new Group();
g.add(new Person("Hans"));
g.add(new Person("Lotte"));
g.add(new Person("Jane"));
g.add(new Person("Hans"));
g.add(new Person("Jane"));
System.out.println("Total: " + g.size());
}
public boolean add(Object o) {
System.out.println("Adding: " + o);
return super.add(o);
}
}
class Person {
private final String name;
public Person(String name) { this.name = name; }
public String toString() { return name; }
}
Which of the following occur at least once when the code is compiled and run?
(Choose all that apply.)
A. Adding Hans B. Adding Lotte
C. Adding Jane D. Total: 3
E. Total: 5 F. The code does not compile.
G. An exception is thrown at runtime.
Answer:
-> F is correct. The problem here is in Group's add() method—it should have been
add(Person), since the class extends HashSet<Person>. So this doesn't compile.
Pop Quiz: What would happen if you fixed this code, changing add(Object) to
add(Person)? Try running the code to see if the results match what you thought.
-> A, B, C, D, E, and G are incorrect based on the above.
56. Given:
import java.util.*;
class AlgaeDiesel {
public static void main(String[] args) {
String[] sa = {"foo", "bar", "baz" };
// insert method invocations here
}
}
What java.util.Arrays and/or java.util.Collections methods could you use to convert sa to a List and then search the List to find the index of the element whose value is "foo"? (Choose from one to three methods.)
A. sort() B. asList()
C. toList() D. search()
E. sortList() F. contains()
G. binarySearch()
Answer:
-> A, B, and G are required. The as List() method converts an array to a List. You can find the index of an element in a List with the binarySearch() method, but before you do that you must sort the list using sort().
-> F is incorrect because contains() returns a boolean, not an index. C, D, and E are incorrect, because these methods are not defined in the List interface. (Objective 6.5)
57. Given that String implements java.lang.CharSequence, and:
import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }
Which declarations could be inserted at // INSERT DECLARATION HERE so that the program will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
B. public static <E extends CharSequence> List<E>
getLongWords(Collection<E> coll)
C. public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
D. public static List<CharSequence>
getLongWords(Collection<CharSequence> coll)
E. public static List<? extends CharSequence>
getLongWords(Collection<? extends CharSequence> coll)
F. static public <E extends CharSequence> Collection<E>
getLongWords(Collection<E> coll)
G. static public <E super CharSequence> Collection<E>
getLongWords(Collection<E> coll)
Answer:
-> F is correct.
-> A is close, but it's wrong because the return value is too vague. The last line of the
method expects the return value to be Collection<String>, not
Collection<? extends CharSequence>. B is wrong because longWords has been declared as a Collection<E>, and that can't be implicitly converted to a List<E> to match the declared return value. (Even though we know that longWords is really an ArrayList<E>, the compiler only know what it's been declared as.) C, D, and E are wrong because they do not declare a type variable E (there's no <> before the return value) so the getLongWords() method body will not compile. G is wrong because E super CharSequence makes no sense—super could be used in conjunction with a wildcard but not a type variable like E.
58. Given:
12. TreeSet map = new TreeSet();
13. map.add("one");
14. map.add("two");
15. map.add("three");
16. map.add("four");
17. map.add("one");
18. Iterator it = map.iterator();
19. while (it.hasNext() ) {
20. System.out.print( it.next() + " " );
21. }
What is the result?
A. Compilation fails.
B. one two three four
C. four three two one
D. four one three two
E. one two three four one
F. one four three two one
G. An exception is thrown at runtime.
H. The print order is not guaranteed.
Answer:
-> D is correct. TreeSet assures no duplicate entries; also, when it is accessed it
will return elements in natural order, which for Strings means alphabetical.
-> A, B, C, E, F, G, and H are incorrect based on the logic described above. Note, even
though as of Java 5 you don't have to use an Iterator, you still can.
59. Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)
A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B. ArrayList<Integer> input = null;
List<Integer> output = null;
C. ArrayList<Integer> input = null;
List<Number> output = null;
D. List<Number> input = null;
ArrayList<Integer> output = null;
E. List<Number> input = null;
List<Number> output = null;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.
Answer:
->B, E, and F are correct.
-> The return type of process is definitely declared as a List, not an ArrayList, so A and D are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>. Of course all these would probably cause a NullPointerException since the variables are still null—but the question only asked us to get the code to compile.
Page: 12
1
2
3
4
5
6
7
8
9
10
11
12