Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 21 Generics
Section 21.2 Motivations
1
Which of the following statements is correct?
A.
Generics can help detect type errors at compile time, thus make programs more robust.
B.
Generics can make programs easy to read.
C.
Generics can avoid cumbersome castings.
D.
Generics can make programs run faster.
2
Fill in the code in Comparable______ c = new Date();
A.
<String>
B.
<?>
C.
<Date>
D.
<E>
3
Which of the following statements is correct?
A.
Comparable<String> c = new String("abc");
B.
Comparable<String> c = "abc";
C.
Comparable<String> c = new Date();
D.
Comparable<Object> c = new Date();
4
Suppose List list = new ArrayList(). Which of the following operations are correct?
A.
list.add("Red");
B.
list.add(new Integer(100));
C.
list.add(new java.util.Date());
D.
list.add(new ArrayList());
5
Suppose List<String> list = new ArrayList<String>. Which of the following operations are correct?
A.
list.add("Red");
B.
list.add(new Integer(100));
C.
list.add(new java.util.Date());
D.
list.add(new ArrayList());
6
Suppose ArrayList<Double>list = new ArrayList<Double>(). Which of the following statements are correct?
A.
list.add(5.5); // 5.5 is automatically converted to new Double(5.5)
B.
list.add(3.0); // 3.0 is automatically converted to new Double(3.0)
C.
Double doubleObject = list.get(0); // No casting is needed
D.
double d = list.get(1); // Automatically converted to double
Section 21.3 Declaring Generic Classes and Interfaces
7
To declare a class named A with a generic type, use
A.
public class A<E> { ... }
B.
public class A<E, F> { ... }
C.
public class A(E) { ... }
D.
public class A(E, F) { ... }
8
To declare a class named A with two generic types, use
A.
public class A<E> { ... }
B.
public class A<E, F> { ... }
C.
public class A(E) { ... }
D.
public class A(E, F) { ... }
9
To declare an interface named A with a generic type, use
A.
public interface A<E> { ... }
B.
public interface A<E, F> { ... }
C.
public interface A(E) { ... }
D.
public interface A(E, F) { ... }
10
To declare an interface named A with two generic types, use
A.
public interface A<E> { ... }
B.
public interface A<E, F> { ... }
C.
public interface A(E) { ... }
D.
public interface A(E, F) { ... }
11
To create a list to store integers, use
A.
ArrayList<Object> list = new ArrayList<Integer>();
B.
ArrayList<Integer> list = new ArrayList<Integer>();
C.
ArrayList<int> list = new ArrayList<int>();
D.
ArrayList<Number> list = new ArrayList<Integer>();
Section 21.4 Generic Methods
12
The method header is left blank in the following code. Fill in the header.
public class GenericMethodDemo {
   public static void main(String[] args ) {
     Integer[] integers = {1, 2, 3, 4, 5};
     String[] strings = {"London", "Paris", "New York", "Austin"};
     print(integers);
     print(strings);
   }
   __________________________________________ {
     for (int i = 0; i < list.length; i++)
       System.out.print(list[i] + " ");
     System.out.println();
   }
}
A.
public static void print(Integer[] list)
B.
public static void print(String[] list)
C.
public static void print(int[] list)
D.
public static void print(Object[] list)
E.
public static <E> void print(E[] list)
13
To create a generic type bounded by Number, use
A.
<E extends Number>
B.
<E extends Object>
C.
<E>
D.
<E extends Integer>
Section 21.5 Raw Type and Backward Compatibility
14
Which of the following declarations use raw type?
A.
ArrayList<Object> list = new ArrayList<Object>();
B.
ArrayList<String> list = new ArrayList<String>();
C.
ArrayList<Integer> list = new ArrayList<Integer>();
D.
ArrayList list = new ArrayList();
15
If you use the javac command to compile a program that contains raw type, what would the compiler do?
A.
report syntax error
B.
report warning and generate a class file
C.
report warning without generating a class file
D.
no error and generate a class file
E.
report warning and generate a class file if no other errors in the program.
16
If you use the javac ?Xlint:unchecked command to compile a program that contains raw type, what would the compiler do?
A.
report syntax error
B.
report warning and generate a class file
C.
report warning without generating a class file
D.
no error and generate a class file
Section 21.6 Wildcards
17
Is ArrayList<Integer> a subclass of ArrayList<Object>?
A.
true
B.
false
18
Is ArrayList<Integer> a subclass of ArrayList<?>?
A.
true
B.
false
19
Is ArrayList<Integer> a subclass of ArrayList<? extends Number>?
A.
true
B.
false
20
Is ArrayList<Number> a subclass of ArrayList<? extends Number>?
A.
true
B.
false
21
Is ArrayList<?> same as ArrayList<? extends Object>?
A.
true
B.
false
22
Does <? super Number> represent a superclass of Number?
A.
yes
B.
no
Section 21.7 Important Facts
23
ArrayList<String> and ArrayList<Integer> are two types. Does the JVM load two classes ArrayList<String> and ArrayList<Integer>?
A.
yes
B.
no
24
Which of the following statements are true?
 
A.
Generic type information is present at compile time.
B.
Generic type information is not present at runtime.
C.
You cannot create an instance using a generic class type parameter.
D.
You cannot create an array using a generic class type parameter.
E.
You cannot create an array using a generic class.
25
If E is a generic type for a class, can E be referenced from a static method?
A.
yes
B.
no