Chapter 18 Generic Classes Exam Prep - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.

Chapter 18 Generic Classes Exam Prep

Course Title: Big Java, Early Objects

Chapter Number: 18 Generic Classes

Question type: Multiple Choice

1. Which of the following statements about generic programming is NOT correct?

a) Generic classes use type parameters to achieve genericity.

b) Generic programming can be achieved by using inheritance.

c) Generic programming can be achieved by using type parameters.

d) To achieve genericity through inheritance, you must include type parameters to specify the type of object to be processed.

Title: Which of the following statements about generic programming is NOT correct?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

2. Which of the following statements about generic programming is NOT correct?

a) Type parameters can be instantiated with class types.

b) Type parameters can be instantiated with interface types.

c) Type parameters can be instantiated with primitive data types.

d) Type parameters can be instantiated with wrapper class types.

Title: Which of the following statements about generic programming is NOT correct?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

3. Consider the following code snippet:

ArrayList<Coin> coins1 = new ArrayList<Coin>(); //Line 1

LinkedList coins2 = new LinkedList(); //Line 2

coins1.add("my penny"); //Line 3

coins2.addFirst("my penny"); //Line 4

Which of the above lines will cause a compile-time error?

a) Line 1

b) Line 2

c) Line 3

d) Line 4

Title: Which line will cause a compile-time error?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

4. Consider the following code snippet:

ArrayList<Coin> coins1 = new ArrayList<Coin>(); //Line 1

LinkedList coins2 = new LinkedList(); //Line 2

coins1.add("my penny"); //Line 3

coins2.addFirst("my penny"); //Line 4

Which of the above lines will cause a run-time error when the object is retrieved elsewhere in the program?

a) Line 1

b) Line 2

c) Line 3

d) Line 4

Title: Which line will cause a run-time error?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

5. Consider the following code snippet:

ArrayList<BankAccount> accounts1 = new ArrayList<BankAccount>(); //Line 1

LinkedList accounts2 = new LinkedList(); //Line 2

accounts1.add("my Salary"); //Line 3

accounts2.addFirst("my Salary"); //Line 4

Which of the above lines will cause a compile-time error?

a) Line 1

b) Line 2

c) Line 3

d) Line 4

Title: Which line will cause a compile-time error?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

6. Consider the following code snippet:

ArrayList<BankAccount> accounts1 = new ArrayList<BankAccount>(); //Line 1

LinkedList accounts2 = new LinkedList(); //Line 2

accounts1.add("my Salary"); //Line 3

accounts2.addFirst("my Salary"); //Line 4

Which of the above lines will cause a run-time error when the object is retrieved elsewhere in the program?

a) Line 1

b) Line 2

c) Line 3

d) Line 4

Title: Which line will cause a run-time error?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

7. Which of the following statements about using generic programming is NOT correct?

a) Using type parameters makes generic code safer.

b) Using type parameters makes generic code easier to read and understand.

c) Using type parameters will potentially avoid some class cast exceptions that may occur when using collections without type parameters.

d) Type parameters cannot be used with interfaces.

Title: Which statement about using generic programming is NOT correct?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

8. Consider the following code snippet:

ArrayList<Double> arr = new ArrayList<Double>();

String element = arr.get(0);

Is there an error in this code?

a) Yes, a run-time error will occur because a Double value cannot be cast to a String value.

b) Yes, a compile-time error will occur because you cannot assign a Double value to a String variable.

c) No run-time error or compile-time errors will occur.

d) You cannot create an arraylist of type Double.

Title: Is there an error in this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

9. Which Java technique(s) allows generic programming?

I type variables

II primitive types

III inheritance

a) I

b) II

c) III

d) I and III

Title: Which Java technique(s) allows generic programming?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

10. Which Java generic programming technique(s) requires the programmer to use casting to access variables stored as Object types?

I type variables

II primitive types

III inheritance

a) I

b) II

c) III

d) I and III

Title: Which Java generic programming technique(s) requires the programmer to use casting to access variables stored as Object types?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

11. Which is the purpose of the <E> element in the class declaration below?

public class Thing<E>

{

public Thing() { . . . }

}

a) declares that only instances of E class can be stored

b) it is a type variable

c) it is an instance variable

d) it is a cast

Title: Which is the purpose of the <E> element in this class declaration?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

12. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

}

Which of the following choices is a specific type of the generic Box class?

a) Box<int>

b) Box<E>

c) Box<Double>

d) Box

Title: Which choice is a specific type of the generic Box class?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

13. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box(){ . . . }

}

Which of the following is a valid Box<E> object instantiation?

a) Box<int> box = new Box<Integer>;

b) Box<E> box = new Box();

c) Box<Double> box = new Box<Double>();

d) Box box = new Box<String>();

Title: Which is a valid Box<E> object instantiation?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

14. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box(){ . . . }

public void insert(E value) { . . . }

}

Which of the following is a valid Box<E> object instantiation?

I Box<Object> box = new Box<Object>();

II Box<Boolean> box = new Box<Boolean>();

III Box<double> box = new Box<double>();

a) I

b) I and II

c) I and III

d) II and III

Title: Which is a valid Box<E> object instantiation?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

15. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

public void insert(E value) { . . . }

}

What will result from executing the following code?

Box<Boolean> box = new Box<Boolean>();

box.insert("blue Box");

a) compile-time error

b) run-time error

c) correct generic assignment

d) compile-time warning

Title: Which is the result of this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

16. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

public void insert(E value) { . . . }

public E getData() { . . . }

}

What will result from executing the following code?

Box<Boolean> box = new Box<Boolean>();

Box b = (Box) box.getData();

a) compile-time error

b) run-time error

c) correct generic assignment

d) compile-time warning

Title: What will result from this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

17. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box(){ . . . }

public void insert(E value) { . . . }

public E getData() { . . . }

}

What will result from executing the following code?

Box<String> box = new Box<String>();

. . .

box.insert("blue Box");

String b = (String) box.getData();

a) compiler error

b) run-time error

c) correct, but unnecessary cast

d) correct, with necessary cast

Title: What will result from this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

18. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box(){ . . . }

public void insert(E value) { . . . }

public E getData(){ . . . }

}

What will result from executing the following code?

Box<String> box = new Box<String>();

. . .

box.insert("blue Box");

String b = (Object) box.getData();

a) compiler error

b) run-time error

c) correct, but unnecessary cast

d) correct, with necessary cast

Title: What will result from this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

19. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

public void insert(E value) { . . . }

public E getData(){ . . . }

}

What will result from executing the following code?

Box<String> box = new Box<String>();

. . .

box.insert("blue Box");

String b = box.getData();

a) compiler error

b) run-time error

c) no error

d) compiler warning

Title: What will result from this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

20. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box() { . . . }

public void insert(E value) { . . . }

public E getData(){ . . . }

}

What will result from the following code?

Box<String> box = new Box<String>();

. . .

box.insert("blue Box");

Double myDouble = (Double) box.getData();

a) compiler error

b) run-time error

c) correct, but unnecessary cast

d) correct, with necessary cast

Title: What will result from this code?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

21. Consider the following code snippet:

public class Box<E>

{

private E data;

public Box(){ . . . }

public void insert(E value) { . . . }

public E getData(){ . . . }

}

What specific exception will be thrown when the following code executes?

Box<String> box = new Box<String>();

. . .

box.insert("blue Box");

Double myDouble = (Double) box.getData();

a) Exception

b) NoSuchElementException

c) IndexOutOfBoundsException

d) ClassCastException

Title: What exception will be thrown?

Difficulty: Medium

Section Reference 1: 18.1 Generic Classes and Type Parameters

22. Which of these Java library classes are implemented using type variables?

I HashMap

II LinkedList

III ArrayList

a) I and II

b) II and III

c) I and III

d) I, II and III

Title: Which classes are implemented using type variables?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

23. The type variables in HashMap<K, V> in the standard library mnemonically represent ____.

a) kernel and vector

b) key and value

c) kind and variable

d) String and Vector

Title: The type variables in the standard library HashMap<K, V> mnemonically represent ____.

Difficulty: Easy

Section Reference 1: 18.2 Implementing Generic Types

24. Consider the following declaration:

LinkedList<String> list = new LinkedList<String>();

This declaration implies that the LinkedList class could begin with which of the following code statements?

I public class LinkedList<String> { . . . }

II public class LinkedList<S> { . . . }

III public class LinkedList<Element> { . . . }

a) I

b) II

c) III

d) II and III

Title: What code could the LinkedList class begin with given this declaration?

Difficulty: Easy

Section Reference 1: 18.2 Implementing Generic Types

25. In Java, generic programming can be achieved with ____.

a) encapsulation

b) inheritance

c) polymorphism

d) arrays

Title: Generic programming can be achieved with ____.

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

26. What is the best technique for overcoming the restriction for the use of primitives, such as int, in generic classes and methods?

a) use of the Object type

b) use of wrapper classes

c) use String and decode values with a method such as Integer.parseInt

d) do not use generic methods when using primitives

Title: What is the best technique for overcoming the restriction for the use of primitives, such as int, in generic classes and methods?

Difficulty: Easy

Section Reference 1: 18.1 Generic Classes and Type Parameters

27. Determine the correctness of the MyLinkedList generic class code below.

public class MyLinkedList<E>

{

private MyNode first;

public E getFirst() { return first.data; }

private class MyNode

{

private E data;

private MyNode next;

}

}

a) MyNode cannot refer to type variable E

b) first.data will cause a compiler error

c) the inner class MyNode cannot be private

d) the code is correct

Title: Determine the correctness of this code using generic programming.

Difficulty: Medium

Section Reference 1: 18.2 Implementing Generic Types

28. Determine the output of the MyLinkedList generic class code below when the main method executes.

public class MyLinkedList<E>

{

private MyNode first;

public MyLinkedList(E e)

{

first = new MyNode();

first.data = e;

first.next = null;

}

public E getFirst() { return first.data; }

private class MyNode

{

private E data;

private MyNode next;

}

public static void main(String[] args)

{

MyLinkedList<String> list = new MyLinkedList<String>("Hello");

System.out.println("List first element = " + list.getFirst());

}

}

a) List first element = Hello

b) List first element = null

c) compiler error is generated by first = new MyNode();

d) no output

Title: Determine the output of this code using a generic class.

Difficulty: Medium

Section Reference 1: 18.2 Implementing Generic Types

29. An inner helper class, such as a TreeNode inside the generic BinaryTree class, must meet which of the following criteria?

I be public

II be private

III be declared as generic

a) I and III

b) II and III

c) I, II and III

d) none are necessary

Title: An inner helper class inside a generic class must meet which criteria?

Difficulty: Medium

Section Reference 1: 18.2 Implementing Generic Types

30. Which of the following statements about generic methods is correct?

a) A generic method must reside in a generic class.

b) A generic method must have a generic type argument.

c) A generic method must have a generic type parameter.

d) The generic type parameter of a generic method designates the method's return type.

Title: Which statement about generic methods is correct?

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

31. Which of the following statements about generic methods is correct?

a) You must instantiate the type parameter when calling a generic method.

b) You must specify which type to use when calling a generic method.

c) Generic methods cannot be static methods.

d) You cannot replace type parameters with primitive types.

Title: Which statement about generic methods is correct?

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

32. Select the correct header for this generic print method.

public static void print(E[] a)

{

for (int i = 0; i < a.length; i++)

{

System.out.println(a[i] + " ");

}

}

a) public static <E> void print(E[] a)

b) public void print(E [] a)

c) public static <E> void print(E a)

d) the header is correct

Title: Select the correct header for this generic print method.

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

33. Consider the following code snippet:

public static <E> void print(E [] a)

{

for (int i = 0; i < a.length; i++)

{

System.out.println(a[i] + " ");

}

}

int[] a = {3,6,5,7,8,9,2,3};

String[] s = {"happy","cat","silly","dog"};

Boolean[] b = {true, true, false};

Which of the following are correct calls to this generic print method?

I print(a);

II print(s);

III print(b);

a) I

b) I and II

c) II and III

d) I, II and III

Title: Which are correct calls to this generic print method?

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

34. Consider the following code snippet:

public static <E> void print(E[] a)

{

for (int i = 0; i < a.length; i++)

{

System.out.println(a[i] + " ");

}

}

int[] a = {3,6,5,7,8,9,2,3};

print(makeArray(a));

Assume that the method call to print(makeArray(a)) works correctly by printing the int array a. Which of the following headers for the makeArray method will make this possible?

I public static Integer[] makeArray(int[] a)

II public static E[] makeArray(int[] a)

III public static Integer[] makeArray(E[] a)

a) I

b) I and II

c) II and III

d) I and III

Title: Which of the following headers for the makeArray method will accomplish this?

Difficulty: Hard

Section Reference 1: 18.3 Generic Methods

35. Which argument type cannot passed to generic methods?

I Object

II GUI components

III primitive

a) I

b) II

c) III

d) I and III

Title: TB Which argument type cannot passed to generic methods?

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

36. Which of the following statements regarding restrictions for generic methods are true?

I Generic methods must be declared inside a generic class.

II Generic methods must be static.

III Generic methods may only have one generic parameter.

a) I

b) I and II

c) II and III

d) none are restrictions

Title: Which statements about restrictions for generic methods are true?

Difficulty: Medium

Section Reference 1: 18.3 Generic Methods

37. Which of the following headers for a generic method myMethod allows the actionPerformed method from the ActionListener class to be called?

I public static <E extends ActionListener> E myMethod(E e)

II public static <E implements ActionListener> E myMethod(E e)

III public <E extends ActionListener> E myMethod(E e)

a) I

b) II

c) III

d) I and III

Title: Which headers for a generic method myMethod allows the actionPerformed method class to be called?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

38) Consider our own generic class MyLinkedList shown below. It has a private Node class, and it implements the standard Java ListIterator generic interface.

public class MyLinkedList<E>

{

private MyNode first;

. . .

private class MyNode

{

private E data;

private MyNode next;

}

private class MyIterator implements ListIterator<E>

{

. . .

}

}

Which of the following statements apply?

I the code is correct

II change to private class MyIterator implements ListIterator

III change to private class MyNode<E>

a) I

b) II

c) III

d) II and III

Title: Which statements apply to this generic class MyLinkedList?

Difficulty: Hard

Section Reference 1: 18.3 Generic Methods

39. What is known for certain about a type parameter when a method constrains it as follows?

<E extends MouseListener & MouseMotionListener>

a) it is either a MouseListener or MouseMotionListener implementer

b) it implements MouseListener and MouseMotionListener

c) it is of type MouseEvent

d) the code is illegal; the & symbol should read and

Title: What is known for certain about a type parameter when a method constrains it?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

40. What is known for certain about Visualizer when a method constrains its type parameter as follows?

<E extends Visualizer>

a) Visualizer must refer to a class

b) Visualizer must refer to an interface

c) Visualizer can refer to either an interface or a class

d) Visualizer is another generic type

Title: What is known about Visualizer when a method constrains its type parameter as shown?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

41. Consider the following code snippet that declares the GraduateStudent class:

public GraduateStudent extends Student { . . .}

Which of these statements are false?

I GraduateStudent is a subclass of Student

II Stack<GraduateStudent> is a subclass of Stack<Student>

III Stack<Student> is a subclass of Stack<GraduateStudent>

a) I

b) II

c) III

d) II and III

Title: Which statements are false if the GraduateStudent class is declared as shown?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

42. Given the following generic method, which of the following is a possible return type?

public static <E extends Comparable> E max(E[] a) { . . . }

I String

II Object

III Double

a) I

b) II

c) I and II

d) I and III

Title: Given the following generic method, which of the following is a possible return type?

Difficulty: Hard

Section Reference 1: 18.4 Constraining Type Parameters

43. Given the following generic method, which of the following CANNOT be the return type?

public static <E extends Comparable> E max(E[] a) { . . .}

I String

II Integer

III double

a) I

b) II

c) III

d) I and III

Title: Given the following generic method, which of the following cannot be the return type?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

44. What does the following code snippet mean:

<E extends Comparable<E> & Measurable>

a) The class represented by E extends Comparable and Measurable.

b) The class represented by E implements Comparable and Measurable.

c) The class represented by E implements Comparable and extends Measurable.

d) The class represented by E extends Comparable and implements Measurable.

Title: What does this code mean?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

45. Consider the following code snippet:

public static <E extends Comparable<E>> E min(ArrayList<E> objects)

What can we conclude about the return type of this method?

a) The return type is a class that extends Comparable.

b) The return type is a class that implements Comparable.

c) The return type is an array list of generic objects.

d) The return type is a subclass of the ArrayList class.

Title: What can we conclude about the return type of this method?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

46. Consider the following class declaration:

public class SavingsAccount extends BankAccount { . . . }

Which of the following statements about these classes is correct?

a) ArrayList<BankAccount> is a subclass of ArrayList<SavingsAccount>.

b) ArrayList<SavingsAccount> is a subclass of ArrayList<BankAccount>.

c) ArrayList<SavingsAccount> extends ArrayList<BankAccount>.

d) There is no relationship between ArrayList<BankAccount> and ArrayList<SavingsAccount>

Title: Which statement about these classes is correct?

Difficulty: Medium

Section Reference 1: Common Error 18.1 Genericity and Inheritance

47. Consider the following code snippet:

public class SavingsAccount extends BankAccount { . . . }

public class CheckingAccount extends BankAccount { . . . }

. . .

SavingsAccount[] savingsAccounts = new SavingsAccount[100]; //Line 1

BankAccount bankAccounts = savingsAccounts; //Line 2

BankAccount myAccount = new CheckingAccount(); //Line 3

bankAccounts[0] = myAccount; //Line 4

Which of the following statements regarding this code is correct?

a) Line 2 will cause a compile-time error.

b) Line 2 will cause a run-time error.

c) Line 4 will cause a compile-time error.

d) Line 4 will cause a run-time error.

Title: Which statement about this code is correct?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Common Error 18.2 The Array Store Exception

48. Suppose a linked-list class with a generic E type has been constructed with a java.awt.Component type variable. Consider its instance method locate with the following header:

public void locate(MyLinkedList<? extends E>) { . . . }

Which type cannot be passed to method locate?

I MyLinkedList<JButton>

II MyLinkedList<Component>

III MyLinkedList<JTextField>

a) I

b) I and II

c) II and III

d) all can be passed to locate

Title: Which type cannot be passed to method locate if a class is constructed with a java.awt.Component type variable?

Difficulty: Hard

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

49) Which code is the equivalent of the following method header?

public static <E> void abc(Stack<E> stack) { . . . }

I public static void abc(Stack<?> stack) { . . . }

II public static <Object> void abc (Stack<Object> stack) { . . . }

III public static void abc(Stack stack) { . . . }

a) I

b) II

c) III

d) I and III

Title: Which code is the equivalent of this method header?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

50) Which of the following is not a wildcard type?

a) ?

b) ? super B

c) ? sub B

d) ? extends B

Title: Which of these is not a wildcard type?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

51) Which of the following satisfies the wildcard ? super Object?

a) String

b) JComponent

c) Scanner

d) none satisfy this wildcard

Title: Which of these satisfies the wildcard ? super Object?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

52) Which of the following satisfies the wildcard ? extends Object?

I String

II JComponent

III Scanner

a) I

b) II

c) I and II

d) I, II and III

Title: Which of these satisfies the wildcard ? extends Object?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

53) Which of the following satisfies the wildcard ? extends Component?

a) String

b) Scanner

c) JButton

d) Color

Title: Which of the following satisfies the wildcard ? extends Component?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

54) What does it mean when the syntax ? extends D is used?

a) Any superclass of D may be used.

b) Any subclass of D may be used.

c) Any subclass or superclass of D may be used.

d) This indicates a wildcard with an upper bound

Title: What does it mean when the syntax ? extends D is used?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

55) What does it mean when the syntax ? super D is used?

a) Any superclass of D may be used.

b) Any subclass of D may be used.

c) Any subclass or superclass of D may be used.

d) This indicates a wildcard with a lower bound

Title: What does it mean when the syntax ? super D is used?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

56) Consider the following code snippet in the LinkedList<E> class:

public void addAll(LinkedList<? extends E> other)

{

ListIterator<E> iter = other.listIterator();

while (iter.hasNext())

{

add(iter.next());

}

}

Which of the following statements about this code is correct?

a) You must supply a specific type for the element type of other.

b) For the element type of other, you must supply a type that is a subtype of E.

c) For the element type of other, you must supply a type that is a supertype of E.

d) For the element type of other, you must supply a type that is either a subtype or a supertype of E.

Title: Which statement about this code is correct?

Difficulty: Medium

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

57) Consider the following code snippet:

public static void reverse(List<?> list) { . . . }

Which of the following statements about this code is correct?

a) This is an example of a wildcard with an upper bound.

b) This is an example of a wildcard with a lower bound.

c) This is an example of an unbounded wildcard.

d) This code is illegal.

Title: Which statement about this code is correct?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type Parameters

Section Reference 2: Special Topic 18.1 Wildcard Types

58) Consider the following code snippet:

public static void reverse(List<? extends E> list) { . . . }

Which of the following statements about this code is correct?

a) This is an example of a wildcard with an upper bound.

b) This is an example of a wildcard with a lower bound.

c) This is an example of an unbounded wildcard.

d) This code is illegal.

Title: Which statement about this code is correct?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type ParametersSection Reference 2: Special Topic 18.1 Wildcard Types

59) Consider the following code snippet:

public static void reverse(List<? super E> list) { . . . }

Which of the following statements about this code is correct?

a) This is an example of a wildcard with an upper bound.

b) This is an example of a wildcard with a lower bound.

c) This is an example of an unbounded wildcard.

d) This code is illegal.

Title: Which statement about this code is correct?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type ParametersSection Reference 2: Special Topic 18.1 Wildcard Types

60) Consider the following code snippet:

public static void reverse(List<?> list) { . . . }

This method declaration is equivalent to which of the following declarations?

a) public static void reverse(List<? extends E> list) { . . . }

b) public static <List> void reverse(List list) { . . . }

c) public static <D> void reverse(List<D> list) { . . . }

d) public static void reverse(List<? super E> list) { . . . }

Title: This generic method declaration is equivalent to which declaration?

Difficulty: Easy

Section Reference 1: 18.4 Constraining Type ParametersSection Reference 2: Special Topic 18.1 Wildcard Types

61) Which of the following statements about the Java virtual machine is correct?

a) The Java virtual machine always replaces type parameters with their upper bounds.

b) The Java virtual machine always replaces type parameters with their lower bounds.

c) The Java virtual machine always replaces type parameters with type Object.

d) The Java virtual machine replaces type parameters with their bounds or with type Object.

Title: Which statement about type erasure is correct?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

62) To maintain compatibility with pre-generic Java, type parameters are replaced by ordinary types called ____.

a) primitives

b) raw types

c) Object

d) generics

Title: To maintain compatibility with pre-generic Java, type parameters are replaced by ordinary types called ____.

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

63) Erasure of types limits Java code somewhat with generics. Which of the following are limitations of generic code?

I cannot instantiate a type variable

II cannot return a type variable

III cannot pass a type variable

a) I

b) II

c) III

d) II and III

Title: Which of these are limitations of generic code?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

64) Consider the following code snippet:

public static <T> void fun(T[] t) { . . . }

Erasure by the compiler of method fun will generate which result?

a) public static <T> void fun(Object[] t) { . . . }

b) public static <Object> void fun(Object t) { . . . }

c) public static void fun(Object[] t) { . . . }

d) public static void fun(Object t) { . . . }

Title: Erasure by the compiler of method fun will generate which result?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

65) Generics limit Java code somewhat. Which of the following are considered limitations of generic code?

I cannot have an array of a generic type

II cannot have primitive type variables

III cannot construct an object of a generic type

a) I

b) II

c) III

d) I, II and III

Title: Which of these are limitations of generic code?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure

66) Which of the following necessitates the type erasure process?

I The Java virtual machine does not work with generic types

II To maintain backward compatibility to older versions of Java

III Generics do not work with primitives

a) I

b) II

c) I and II

d) I and III

Title: Which of these necessitates the type erasure process?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

67) Suppose a generic method accepts a generic unconstrained argument p. What restrictions are placed on p by the compiler?

a) Can only execute Object class methods.

b) Can only execute methods from its own class.

c) Can not execute any methods.

d) There are no restrictions.

Title: Suppose a generic method accepts a generic unconstrained parameter p. What restrictions are placed on a generic unconstrained argument p by the compiler?

Difficulty: Hard

Section Reference 1: 18.5 Type Erasure

68) Which of the following are restrictions of Java generic programming?

I You cannot use the assignment operator = to assign one generic object to another.

II You cannot use the == operator to compare two generic objects.

III You cannot construct arrays of generic types.

a) I

b) II

c) III

d) I and III

Title: Which of these are restrictions of Java generic programming?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

69) Given the following declaration, what is the type parameter in Stack<E> replaced with?

private Stack myStack = new Stack();

a) String

b) Object

c) int

d) it is a compiler error

Title: Given the following declaration, what is the type variable replaced with?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure

70) Given the following declaration, what is the type parameter in Stack<E> replaced with?

private Stack<String> myStack = new Stack<String>();

a) String

b) Object

c) int

d) Stack<String>

Title: Given the following declaration, what is the type variable replaced with?

Difficulty: Easy

Section Reference 1: 18.5 Type Erasure

71) What is the result when a program constructs a generic class without passing a type variable, as in the following code?

Stack stack = new Stack();

I the program will compile and run

II the compiler will issue an error

III the compiler will issue a warning

a) I

b) II

c) III

d) I and III

Title: What is the result when a program constructs a generic class without passing a type variable?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure

72) Consider the following code snippet:

public interface MyInterface<E> { . . . }

public interface YourInterface<E, T> extends MyInterface<E> { . . . }

Which of these are legal class declarations?

I public class SomeClass implements YourInterface<String, Double> { . . . }

II public class SomeClass implements YourInterface { . . . }

III public class SomeClass implements YourInterface<String> { . . . }

a) I

b) III

c) I and II

d) I and III

Title: Which of these are legal class declarations?

Difficulty: Hard

Section Reference 1: 18.5 Type Erasure

73) If a class requires two generic type variables, how many can you legally provide in Java?

I 0

II 1

III 2

a) I

b) II

c) I and II

d) I and III

Title: If a class requires two generic type variables, how many can you legally provide in Java?

Difficulty: Hard

Section Reference 1: 18.5 Type Erasure

74) Consider the following code snippet:

public static <E extends Measurable> E min(E[] objects)

Which of the following represents the result of type erasure on this code?

a) public static Measurable E min(Measurable E[] objects)

b) public static Measurable min(Measurable[] objects)

c) public static <Measurable E> E min(Measurable E[] objects)

d) This code is illegal and type erasure will not occur.

Title: Which of these represents the result of type erasure on this code?

Difficulty: Hard

Section Reference 1: 18.5 Type Erasure

75) Generics limit Java code somewhat. Which of the following are limitations of generic code?

I cannot declare static variables of a generic type

II cannot declare static methods of a generic type

III cannot declare static inner classes of a generic type

a) I

b) II

c) I and II

d) I, II, and III

Title: Which of these are limitations of generic code?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure Section Reference 2: Common Error 18.3 Using Generic Types in a Static Context

76) Consider the following code snippet:

public class LinkedList<E>

{

private E defaultValue; //Line 1

public void add(E value, int n) { . . . } //Line 2

private static class Node { public E data; public Node next;)//Line 3

. . .

}

What is wrong with this code?

a) Cannot declare the variable defaultValue as a generic type.

b) Cannot pass a generic type as an argument to a method.

c) Cannot have a static method in a generic class as shown.

d) Cannot have a static inner class in a generic class as shown.

Title: What is wrong with this class?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure Section Reference 2: Common Error 18.3 Using Generic Types in a Static Context

77) Consider the following code snippet:

public class LinkedList<E>

{

private static E defaultValue; //Line 1

public void add(E value, int n) { . . . } //Line 2

private class Node { public String data; public Node next;)//Line 3

. . .

}

What is wrong with this code?

a) Cannot declare the variable defaultValue as static.

b) Cannot pass a generic type as an argument to a method as shown.

c) Cannot have an inner class in a generic class as shown.

d) Cannot declare the variable defaultValue as a generic data type.

Title: What is wrong with this class?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure Section Reference 2: Common Error 18.3 Using Generic Types in a Static Context

78) Consider the following code snippet:

public class LinkedList<E>

{

private E defaultValue;

public static List<E> replicate(E value, int n) { . . . }

private class Node { public String data; public Node next;)

. . .

}

What is wrong with this code?

a) Cannot declare the variable defaultValue as a generic type.

b) Cannot pass a generic type as an argument to a method.

c) Cannot have a static method in a generic class as shown.

d) Cannot have an inner class in a generic class.

Title: Which of these are limitations of generic code?

Difficulty: Medium

Section Reference 1: 18.5 Type Erasure Section Reference 2: Common Error 18.3 Using Generic Types in a Static Context

Document Information

Document Type:
DOCX
Chapter Number:
18
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 18 Generic Classes
Author:
Cay S. Horstmann

Connected Book

Big Java Early Objects 5e Complete Test Bank

By Cay S. Horstmann

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party