Chapter 19 Check Point Questions
Section 19.2
▼19.2.1
Are there any compile errors in (a) and (b)?
(a) Prior to JDK 1.5 ArrayList dates = new ArrayList(); dates.add(new Date()); dates.add(new String()); (b) Since JDK 1.5 ArrayList<Date> dates = new ArrayList<>(); dates.add(new Date()); dates.add(new String());
▼19.2.2
What is wrong in (a)? Is the code in (b) correct?
(a) Prior to JDK 1.5 ArrayList dates = new ArrayList(); dates.add(new Date()); Date date = dates.get(0); (b) Since JDK 1.5 ArrayList<Date> dates = new ArrayList<>(); dates.add(new Date()); Date date = dates.get(0);
▼19.2.3
What are the benefits of using generic types?
Section 19.3
▼19.3.1
What is the generic definition for java.lang.Comparable in the Java API?
▼19.3.2
Since you create an instance of ArrayList of strings using new ArrayList<String>(), should the constructor in the ArrayList class be defined as
public ArrayList<E>()
▼19.3.3
Can a generic class have multiple generic parameters?
▼19.3.4
How do you declare a generic type in a class?
Section 19.4
▼19.4.1
How do you declare a generic method? How do you invoke a generic method?
▼19.4.2
What is a bounded generic type?
Section 19.5
▼19.5.1
Given int[] list = {1, 2, -1}, can you invoke sort(list) using the sort method in Listing 19.4?
▼19.5.2
Given int[] list = {new Integer(1), new Integer(2), new Integer(-1)}, can you invoke sort(list) using the sort method in Listing 19.4?
Section 19.6
▼19.6.1
What is a raw type? Why is a raw type unsafe? Why is the raw type allowed in Java?
▼19.6.2
What is the syntax to declare an ArrayList reference variable using the raw type and assign a raw type ArrayList object to it?
Section 19.7
▼19.7.1
Is GenericStack the same as GenericStack<Object>?
▼19.7.2
What are an unbounded wildcard, a bounded wildcard,
and a lower-bound wildcard?
▼19.7.3
What happens if lines 12-13 in Listing 19.9 are changed to
public static <T> void add(GenericStack<T> stack1, GenericStack<T> stack2)
▼19.7.4
What happens if lines 12-13 in Listing 19.9 are changed to
public static <T> void add(GenericStack<? extends T> stack1, GenericStack<T> stack2)
Section 19.8
▼19.8.1
What is erasure? Why are Java generics implemented using erasure?
▼19.8.2
If your program uses ArrayList<String> and ArrayList<Date> , does the JVM load both of them?
▼19.8.3
Can you create an instance using new E() for a generic type E? Why?
▼19.8.4
Can a method that uses a generic class parameter be static? Why?
▼19.8.5
Can you define a custom generic exception class? Why?
Section 19.9
▼19.9.1
Why are the add, multiple, and zero methods defined abstract in the GenericMatrix class?
▼19.9.2
How are the add, multiple, and zero methods implemented in the IntegerMatrix class?
▼19.9.3
How are the add, multiple, and zero methods implemented in the RationalMatrix class?
▼19.9.4
What would be wrong if the printResult method defined as follows?
public static void printResult( E[][] m1, E[][] m2, E[][] m3, char op)