Deck 18: Generic Classes

Full screen (f)
exit full mode
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
}
Which of the following choices is a specific type of the generic Box class?
A.Box
B.Box
C.Box
D.Box
Use Space or
up arrow
down arrow
to flip the card.
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
}
Which of the following is a valid Box object instantiation?
A.Box box = new Box();
B.Box box = new Box<>();
C.Box box = new Box<>;
D.Box box = new Box<>();
Question
Which of the following statements about generic programming is NOT correct?
A.Generic programming can be achieved by using type parameters.
B.Generic classes use type parameters to achieve genericity.
C.To achieve genericity through inheritance, you must include type parameters to specify the type of object to be processed.
D.Generic programming can be achieved by using inheritance.
Question
Which is the purpose of the element in the class declaration below?
public class Thing
{
public Thing() { ...}
}
A.declares that only instances of E class can be stored
B.it is a type parameter
C.it is a cast
D.it is an instance variable
Question
In Java, generic programming can be achieved with ____.
A.polymorphism
B.arrays
C.encapsulation
D.inheritance
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
}
Which of the following is a valid Box object instantiation?
I Box box = new Box<>();
II Box box = new Box<>();
III Box box = new Box<>();
A.II and III only
B.I and II only
C.I and III only
D.I only
Question
Consider the following code snippet:
public class Box
{
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 box = new Box<>();
...
box.insert("blue Box");
Double myDouble = (Double) box.getData();
A.ClassCastException
B.Exception
C.IndexOutOfBoundsException
D.NoSuchElementException
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
}
What will result from executing the following code?
Box box = new Box<>();
box.insert("blue Box");
A.compile-time warning
B.run-time error
C.compile-time error
D.correct generic assignment
Question
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 and III only
B.III only
C.I only
D.II only
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = (Object) box.getData();
A.correct, with necessary cast
B.compiler error
C.correct, but unnecessary cast
D.run-time error
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
public E getData() { ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = (String) box.getData();
A.correct, with necessary cast
B.run-time error
C.compiler error
D.correct, but unnecessary cast
Question
Which Java technique(s) allows generic programming?
I type variables
II primitive types
III inheritance
A.I only
B.I and III only
C.III only
D.II only
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = box.getData();
A.no error
B.compiler warning
C.run-time error
D.compiler error
Question
Consider the following code snippet:
ArrayList arr = new ArrayList<>();
String element = arr.get(0);
Is there an error in this code?
A.No run-time error or compile-time errors will occur.
B.Yes, a compile-time error will occur because you cannot create an ArrayList of type Double.
C.Yes, a run-time error will occur because a Double value cannot be cast to a String value.
D.Yes, a compile-time error will occur because you cannot assign a Double value to a String variable.
Question
Consider the following code snippet: Consider the following code snippet:   Which of the above lines will cause a compile-time error? A.Line 2 B.Line 3 C.Line 4 D.Line 1<div style=padding-top: 35px> Which of the above lines will cause a compile-time error?
A.Line 2
B.Line 3
C.Line 4
D.Line 1
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData() { ...}
}
What will result from executing the following code?
Box box = new Box<>();
Box b = (Box) box.getData();
A.correct generic assignment
B.run-time error
C.compile-time warning
D.compile-time error
Question
Which of the following statements about generic programming is NOT correct?
A.Type parameters can be instantiated with wrapper class types.
B.Type parameters can be instantiated with class types.
C.Type parameters can be instantiated with primitive data types.
D.Type parameters can be instantiated with interface types.
Question
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from the following code?
Box box = new Box<>();
...
box.insert("blue Box");
Double myDouble = (Double) box.getData();
A.compiler error
B.correct, but unnecessary cast
C.correct, with necessary cast
D.run-time error
Question
Which of these Java library classes are implemented using type variables?
I HashMap
II LinkedList
III ArrayList
A.I, II and III
B.II and III only
C.I and III only
D.I and II only
Question
Which of the following statements about using generic programming is NOT correct?
A.Using type parameters will potentially avoid some class cast exceptions that may occur when using collections without type parameters.
B.Using type parameters makes generic code easier to read and understand.
C.Type parameters cannot be used with interfaces.
D.Using type parameters makes generic code safer.
Question
Which argument type cannot be passed to generic methods?
I Object
II GUI components
III primitive
A.III only
B.I only
C.II only
D.I and III only
Question
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 only
B.I, II and III
C.II and III only
D.none are necessary
Question
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 myMethod(E e)
II public static E myMethod(E e)
III public E myMethod(E e)
A.I only
B.I and III only
C.III only
D.II only
Question
The type variables in HashMap in the standard library mnemonically represent ____.
A.kernel and vector
B.String and Vector
C.kind and variable
D.key and value
Question
Consider the following code snippet:
public static 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.II and III only
B.I and III only
C.I and II only
D.I only
Question
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 is a subclass of Stack
III Stack is a subclass of Stack
A.I only
B.III only
C.II only
D.II and III only
Question
What is known for certain about Visualizer when a method constrains its type parameter as follows?

A.Visualizer can refer to either an interface or a class
B.Visualizer must refer to a class
C.Visualizer must refer to an interface
D.Visualizer is another generic type
Question
What is the best technique for overcoming the restriction for the use of primitives, such as int, in generic classes and methods?
A.do not use generic methods when using primitives
B.use String and decode values with a method such as Integer.parseInt
C.use of wrapper classes
D.use of the Object type
Question
Determine the correctness of the MyLinkedList generic class code below.
public class MyLinkedList
{
private MyNode first;
public E getFirst() { return first.data; }
private class MyNode
{
private E data;
private MyNode next;
}
}
A.the inner class MyNode cannot be private
B.MyNode cannot refer to type variable E
C.first.data will cause a compiler error
D.the code is correct
Question
Which of the following statements about generic methods is NOT correct?
A.Supply the type parameters of a generic method between the modifiers and the method return type.
B.Generic methods can be static methods.
C.You need not instantiate the type parameter when calling a generic method.
D.You can replace type parameters with primitive types.
Question
Determine the output of the MyLinkedList generic class code below when the main method executes.
public class MyLinkedList
{
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 list = new MyLinkedList<>("Hello");
System.out.println("List first element = " + list.getFirst());
}
}
A.compiler error is generated by first = new MyNode();
B.no output
C.List first element = Hello
D.List first element = null
Question
Given the following generic method, which of the following CANNOT be the return type?
public static > E max(E[] a) { ...}
I String
II Integer
III double
A.I only
B.I and III only
C.III only
D.II only
Question
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.The header is correct
B.public void print(E [] a)
C.public static void print(E a)
D.public static void print(E[] a)
Question
Which of the following statements about generic methods is correct?
A.The generic type parameter of a generic method designates the method's return type.
B.A generic method must have a generic type parameter.
C.When calling a generic method, you need to instantiate the type parameters.
D.A generic method must reside in a generic class.
Question
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.none are restrictions
B.II and III only
C.I and II only
D.I only
Question
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
{
private MyNode first;
...
private class MyNode
{
private E data;
private MyNode next;
}
private class MyIterator implements ListIterator
{
...
}
}
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
A.II and III only
B.II only
C.III only
D.I only
Question
Consider the following code snippet:
public static 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.II and III only
B.I and II only
C.I, II and III
D.I only
Question
What is known for certain about a type parameter when a method constrains it as follows?

A.it implements MouseListener and MouseMotionListener
B.it is of type MouseEvent
C.the code is illegal; the & symbol should read and
D.it is either a MouseListener or MouseMotionListener implementer
Question
Given the following generic method, which of the following is a possible return type?
public static > E max(E[] a) { ...}
I String
II Object
III Double
A.I only
B.I and III only
C.II only
D.I and II only
Question
Consider the following declaration:
LinkedList list = new LinkedList<>();
This declaration implies that the LinkedList class could begin with which of the following code statements?
I public class LinkedList { ...}
II public class LinkedList { ...}
III public class LinkedList { ...}
A.II only
B.I only
C.III only
D.II and III only
Question
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 only
B.II only
C.II and III only
D.III only
Question
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 list) { ...}
B.public static void reverse(List list) { ...}
C.public static void reverse(List list) { ...}
D.public static void reverse(List list) { ...}
Question
What does it mean when the syntax ? extends D is used?
A.Any subclass of D may be used.
B.Any superclass of D may be used.
C.This indicates a wildcard with an upper bound.
D.Any subclass or superclass of D may be used.
Question
To maintain compatibility with pre-generic Java, type parameters are replaced by ordinary types called ____.
A.raw types
B.primitives
C.generics
D.Object
Question
Consider the following code snippet:
public static void reverse(List list) { ...}
Which of the following statements about this code is correct?
A.This code is illegal.
B.This is an example of a wildcard with an upper bound.
C.This is an example of a wildcard with a lower bound.
D.This is an example of an unbounded wildcard.
Question
Suppose a linked-list class called MyLinkedList with a generic E type has been instantiated with a java.awt.Component type variable.Consider its instance method locate with the following header:
public void locate(MyLinkedList) { ...}
Which type cannot be passed to method locate?
I MyLinkedList
II MyLinkedList
III MyLinkedList
A.I only
B.I and II only
C.II and III only
D.I, II, and III
Question
Which of the following statements about the Java virtual machine is correct?
A.The Java virtual machine always replaces type parameters with their lower bounds.
B.The Java virtual machine always replaces type parameters with their upper bounds.
C.The Java virtual machine replaces type parameters with their bounds or with type Object.
D.The Java virtual machine always replaces type parameters with type Object.
Question
Which of the following is not a wildcard type?
A.? sub B
B.? extends B
C.? super B
D.?
Question
Consider the following code snippet:
public static > E min(ArrayList objects)
What can we conclude about the return type of this method?
A.The return type is a subclass of the ArrayList class.
B.The return type is a class that implements Comparable.
C.The return type is a class that extends Comparable.
D.The return type is an array list of generic objects.
Question
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 run-time error.
B.Line 4 will cause a compile-time error.
C.Line 4 will cause a run-time error.
D.Line 2 will cause a compile-time error.
Question
Which of the following satisfies the wildcard ? extends Component?
A.Scanner
B.Color
C.String
D.JButton
Question
Which of the following satisfies the wildcard ? extends Object?
I String
II JComponent
III Scanner
A.I and II only
B.I only
C.II only
D.I, II and III
Question
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 an unbounded wildcard.
B.This is an example of a wildcard with a lower bound.
C.This is an example of a wildcard with an upper bound.
D.This code is illegal.
Question
Consider the following class declaration:
public class SavingsAccount extends BankAccount
{ ...}
Which of the following statements about these classes is correct?
A.ArrayList is a subclass of ArrayList.
B.ArrayList is a subclass of ArrayList.
C.There is no relationship between ArrayList and ArrayList
D.ArrayList extends ArrayList.
Question
Consider the following code snippet:
public static void reverse(List list) { ...}
Which of the following statements about this code is correct?
A.This code is illegal.
B.This is an example of an unbounded wildcard.
C.This is an example of a wildcard with an upper bound.
D.This is an example of a wildcard with a lower bound.
Question
Which of the following satisfies the wildcard ? super Object?
A.JComponent
B.None satisfy this wildcard.
C.Scanner
D.String
Question
What does the following code snippet mean:
& Measurable>
A.The class represented by E extends Comparable and Measurable.
B.The class represented by E implements Comparable and extends Measurable.
C.The class represented by E extends Comparable and implements Measurable.
D.The class represented by E implements Comparable and Measurable.
Question
Consider the following code snippet:
public static void fun(T[] t) { ...}
Erasure by the compiler of method fun will generate which result?
A.public static void fun(Object[] t) { ...}
B.public static void fun(Object[] t) { ...}
C.public static void fun(Object t) { ...}
D.public static void fun(Object t) { ...}
Question
Which code is the equivalent of the following method header?
public static void abc(Stack stack) { ...}
I public static void abc(Stack stack) { ...}
II public static void abc (Stack stack) { ...}
III public static void abc(Stack stack) { ...}
A.II only
B.I only
C.III only
D.I and III only
Question
What does it mean when the syntax ? super D is used?
A.Any superclass of D may be used.
B.This indicates a wildcard with a lower bound.
C.Any subclass or superclass of D may be used.
D.Any subclass of D may be used.
Question
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 only
B.I, II and III
C.II only
D.III only
Question
Given the following declaration, what is the type parameter in Stack replaced with?
private Stack myStack = new Stack();
A.String
B.Object
C.it is a compiler error
D.int
Question
Consider the following code snippet:
public class LinkedList
{
private E defaultValue;
public void add(E value, int n) { ...}
private static class Node { public E 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 a static inner class in a generic class as shown.
Question
If a class requires two generic type variables, how many can you legally provide in Java?
I 0
II 1
III 2
A.I and II only
B.I and III only
C.I only
D.II only
Question
Consider the following code snippet:
public class LinkedList
{
private E defaultValue;
public static List replicate(E value, int n) { ...}
private class Node { public String data; public Node next;)
...
}
What is wrong with this code?
A.Cannot pass a generic type as an argument to a method.
B.Cannot have a static method in a generic class as shown.
C.Cannot have an inner class in a generic class.
D.Cannot declare the variable defaultValue as a generic type.
Question
Consider the following code snippet:
public interface MyInterface { ...}
public interface YourInterface extends MyInterface { ...}
Which of these are legal class declarations?
I public class SomeClass implements YourInterface { ...}
II public class SomeClass implements YourInterface { ...}
III public class SomeClass implements YourInterface { ...}
A.I and III only
B.III only
C.I only
D.I and II only
Question
Given an array myArray, which of the following represents an expression to determine the type of the elements of the array?
A.myArray.getClass().getComponentType()
B.myArray.getComponentType().getClass()
C.myArray.getClass().getElementType()
D.myArray[0].getClass()
Question
Suppose a generic method accepts a generic unconstrained argument p.What restrictions are placed on p by the compiler?
A.There are no restrictions.
B.Can only execute methods from its own class.
C.Can only execute Object class methods.
D.Can not execute any methods.
Question
Given an object myObject, which of the following represents the correct code to make a new instance of an object having the same class, assuming that the class has a constructor with no arguments?
A.Object newObject = myObject.newInstance();
B.Object newObject = myObject.newInstance().getClass();
C.Object newObject = myObject.getClass();
D.Object newObject = myObject.getClass().newInstance();
Question
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.II only
B.I and III only
C.I and II only
D.I only
Question
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.II only
B.III only
C.I only
D.I and III only
Question
Given the following declaration, what is the type parameter in Stack replaced with?
private Stack myStack = new Stack<>();
A.Object
B.String
C.Stack
D.int
Question
Consider the following code snippet:
public class LinkedList
{
private static E defaultValue;
public void add(E value, int n) { ...}
private class Node { public String data; public Node next;}
...
}
What is wrong with this code?
A.Cannot pass a generic type as an argument to a method as shown.
B.Cannot have an inner class in a generic class as shown.
C.Cannot declare the variable defaultValue as a generic data type.
D.Cannot declare the variable defaultValue as static.
Question
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.II only
B.I only
C.I and II only
D.I, II, and III
Question
Consider the following code snippet:
public static E min(E[] objects)
Which of the following represents the result of type erasure on this code?
A.public static E min(Measurable E[] objects)
B.public static Measurable min(Measurable[] objects)
C.This code is illegal and type erasure will not occur.
D.public static Measurable E min(Measurable E[] objects)
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/75
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 18: Generic Classes
1
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
}
Which of the following choices is a specific type of the generic Box class?
A.Box
B.Box
C.Box
D.Box
Box
2
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
}
Which of the following is a valid Box object instantiation?
A.Box box = new Box();
B.Box box = new Box<>();
C.Box box = new Box<>;
D.Box box = new Box<>();
Box box = new Box<>();
3
Which of the following statements about generic programming is NOT correct?
A.Generic programming can be achieved by using type parameters.
B.Generic classes use type parameters to achieve genericity.
C.To achieve genericity through inheritance, you must include type parameters to specify the type of object to be processed.
D.Generic programming can be achieved by using inheritance.
To achieve genericity through inheritance, you must include type parameters to specify the type of object to be processed.
4
Which is the purpose of the element in the class declaration below?
public class Thing
{
public Thing() { ...}
}
A.declares that only instances of E class can be stored
B.it is a type parameter
C.it is a cast
D.it is an instance variable
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
5
In Java, generic programming can be achieved with ____.
A.polymorphism
B.arrays
C.encapsulation
D.inheritance
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
6
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
}
Which of the following is a valid Box object instantiation?
I Box box = new Box<>();
II Box box = new Box<>();
III Box box = new Box<>();
A.II and III only
B.I and II only
C.I and III only
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
7
Consider the following code snippet:
public class Box
{
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 box = new Box<>();
...
box.insert("blue Box");
Double myDouble = (Double) box.getData();
A.ClassCastException
B.Exception
C.IndexOutOfBoundsException
D.NoSuchElementException
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
8
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
}
What will result from executing the following code?
Box box = new Box<>();
box.insert("blue Box");
A.compile-time warning
B.run-time error
C.compile-time error
D.correct generic assignment
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
9
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 and III only
B.III only
C.I only
D.II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
10
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = (Object) box.getData();
A.correct, with necessary cast
B.compiler error
C.correct, but unnecessary cast
D.run-time error
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
11
Consider the following code snippet:
public class Box
{
private E data;
public Box(){ ...}
public void insert(E value) { ...}
public E getData() { ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = (String) box.getData();
A.correct, with necessary cast
B.run-time error
C.compiler error
D.correct, but unnecessary cast
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
12
Which Java technique(s) allows generic programming?
I type variables
II primitive types
III inheritance
A.I only
B.I and III only
C.III only
D.II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
13
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from executing the following code?
Box box = new Box<>();
...
box.insert("blue Box");
String b = box.getData();
A.no error
B.compiler warning
C.run-time error
D.compiler error
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
14
Consider the following code snippet:
ArrayList arr = new ArrayList<>();
String element = arr.get(0);
Is there an error in this code?
A.No run-time error or compile-time errors will occur.
B.Yes, a compile-time error will occur because you cannot create an ArrayList of type Double.
C.Yes, a run-time error will occur because a Double value cannot be cast to a String value.
D.Yes, a compile-time error will occur because you cannot assign a Double value to a String variable.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
15
Consider the following code snippet: Consider the following code snippet:   Which of the above lines will cause a compile-time error? A.Line 2 B.Line 3 C.Line 4 D.Line 1 Which of the above lines will cause a compile-time error?
A.Line 2
B.Line 3
C.Line 4
D.Line 1
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
16
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData() { ...}
}
What will result from executing the following code?
Box box = new Box<>();
Box b = (Box) box.getData();
A.correct generic assignment
B.run-time error
C.compile-time warning
D.compile-time error
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
17
Which of the following statements about generic programming is NOT correct?
A.Type parameters can be instantiated with wrapper class types.
B.Type parameters can be instantiated with class types.
C.Type parameters can be instantiated with primitive data types.
D.Type parameters can be instantiated with interface types.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
18
Consider the following code snippet:
public class Box
{
private E data;
public Box() { ...}
public void insert(E value) { ...}
public E getData(){ ...}
}
What will result from the following code?
Box box = new Box<>();
...
box.insert("blue Box");
Double myDouble = (Double) box.getData();
A.compiler error
B.correct, but unnecessary cast
C.correct, with necessary cast
D.run-time error
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
19
Which of these Java library classes are implemented using type variables?
I HashMap
II LinkedList
III ArrayList
A.I, II and III
B.II and III only
C.I and III only
D.I and II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
20
Which of the following statements about using generic programming is NOT correct?
A.Using type parameters will potentially avoid some class cast exceptions that may occur when using collections without type parameters.
B.Using type parameters makes generic code easier to read and understand.
C.Type parameters cannot be used with interfaces.
D.Using type parameters makes generic code safer.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
21
Which argument type cannot be passed to generic methods?
I Object
II GUI components
III primitive
A.III only
B.I only
C.II only
D.I and III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
22
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 only
B.I, II and III
C.II and III only
D.none are necessary
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
23
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 myMethod(E e)
II public static E myMethod(E e)
III public E myMethod(E e)
A.I only
B.I and III only
C.III only
D.II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
24
The type variables in HashMap in the standard library mnemonically represent ____.
A.kernel and vector
B.String and Vector
C.kind and variable
D.key and value
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
25
Consider the following code snippet:
public static 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.II and III only
B.I and III only
C.I and II only
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
26
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 is a subclass of Stack
III Stack is a subclass of Stack
A.I only
B.III only
C.II only
D.II and III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
27
What is known for certain about Visualizer when a method constrains its type parameter as follows?

A.Visualizer can refer to either an interface or a class
B.Visualizer must refer to a class
C.Visualizer must refer to an interface
D.Visualizer is another generic type
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
28
What is the best technique for overcoming the restriction for the use of primitives, such as int, in generic classes and methods?
A.do not use generic methods when using primitives
B.use String and decode values with a method such as Integer.parseInt
C.use of wrapper classes
D.use of the Object type
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
29
Determine the correctness of the MyLinkedList generic class code below.
public class MyLinkedList
{
private MyNode first;
public E getFirst() { return first.data; }
private class MyNode
{
private E data;
private MyNode next;
}
}
A.the inner class MyNode cannot be private
B.MyNode cannot refer to type variable E
C.first.data will cause a compiler error
D.the code is correct
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
30
Which of the following statements about generic methods is NOT correct?
A.Supply the type parameters of a generic method between the modifiers and the method return type.
B.Generic methods can be static methods.
C.You need not instantiate the type parameter when calling a generic method.
D.You can replace type parameters with primitive types.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
31
Determine the output of the MyLinkedList generic class code below when the main method executes.
public class MyLinkedList
{
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 list = new MyLinkedList<>("Hello");
System.out.println("List first element = " + list.getFirst());
}
}
A.compiler error is generated by first = new MyNode();
B.no output
C.List first element = Hello
D.List first element = null
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
32
Given the following generic method, which of the following CANNOT be the return type?
public static > E max(E[] a) { ...}
I String
II Integer
III double
A.I only
B.I and III only
C.III only
D.II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
33
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.The header is correct
B.public void print(E [] a)
C.public static void print(E a)
D.public static void print(E[] a)
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following statements about generic methods is correct?
A.The generic type parameter of a generic method designates the method's return type.
B.A generic method must have a generic type parameter.
C.When calling a generic method, you need to instantiate the type parameters.
D.A generic method must reside in a generic class.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
35
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.none are restrictions
B.II and III only
C.I and II only
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
36
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
{
private MyNode first;
...
private class MyNode
{
private E data;
private MyNode next;
}
private class MyIterator implements ListIterator
{
...
}
}
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
A.II and III only
B.II only
C.III only
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
37
Consider the following code snippet:
public static 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.II and III only
B.I and II only
C.I, II and III
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
38
What is known for certain about a type parameter when a method constrains it as follows?

A.it implements MouseListener and MouseMotionListener
B.it is of type MouseEvent
C.the code is illegal; the & symbol should read and
D.it is either a MouseListener or MouseMotionListener implementer
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
39
Given the following generic method, which of the following is a possible return type?
public static > E max(E[] a) { ...}
I String
II Object
III Double
A.I only
B.I and III only
C.II only
D.I and II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
40
Consider the following declaration:
LinkedList list = new LinkedList<>();
This declaration implies that the LinkedList class could begin with which of the following code statements?
I public class LinkedList { ...}
II public class LinkedList { ...}
III public class LinkedList { ...}
A.II only
B.I only
C.III only
D.II and III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
41
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 only
B.II only
C.II and III only
D.III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
42
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 list) { ...}
B.public static void reverse(List list) { ...}
C.public static void reverse(List list) { ...}
D.public static void reverse(List list) { ...}
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
43
What does it mean when the syntax ? extends D is used?
A.Any subclass of D may be used.
B.Any superclass of D may be used.
C.This indicates a wildcard with an upper bound.
D.Any subclass or superclass of D may be used.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
44
To maintain compatibility with pre-generic Java, type parameters are replaced by ordinary types called ____.
A.raw types
B.primitives
C.generics
D.Object
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
45
Consider the following code snippet:
public static void reverse(List list) { ...}
Which of the following statements about this code is correct?
A.This code is illegal.
B.This is an example of a wildcard with an upper bound.
C.This is an example of a wildcard with a lower bound.
D.This is an example of an unbounded wildcard.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
46
Suppose a linked-list class called MyLinkedList with a generic E type has been instantiated with a java.awt.Component type variable.Consider its instance method locate with the following header:
public void locate(MyLinkedList) { ...}
Which type cannot be passed to method locate?
I MyLinkedList
II MyLinkedList
III MyLinkedList
A.I only
B.I and II only
C.II and III only
D.I, II, and III
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
47
Which of the following statements about the Java virtual machine is correct?
A.The Java virtual machine always replaces type parameters with their lower bounds.
B.The Java virtual machine always replaces type parameters with their upper bounds.
C.The Java virtual machine replaces type parameters with their bounds or with type Object.
D.The Java virtual machine always replaces type parameters with type Object.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
48
Which of the following is not a wildcard type?
A.? sub B
B.? extends B
C.? super B
D.?
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
49
Consider the following code snippet:
public static > E min(ArrayList objects)
What can we conclude about the return type of this method?
A.The return type is a subclass of the ArrayList class.
B.The return type is a class that implements Comparable.
C.The return type is a class that extends Comparable.
D.The return type is an array list of generic objects.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
50
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 run-time error.
B.Line 4 will cause a compile-time error.
C.Line 4 will cause a run-time error.
D.Line 2 will cause a compile-time error.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
51
Which of the following satisfies the wildcard ? extends Component?
A.Scanner
B.Color
C.String
D.JButton
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
52
Which of the following satisfies the wildcard ? extends Object?
I String
II JComponent
III Scanner
A.I and II only
B.I only
C.II only
D.I, II and III
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
53
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 an unbounded wildcard.
B.This is an example of a wildcard with a lower bound.
C.This is an example of a wildcard with an upper bound.
D.This code is illegal.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
54
Consider the following class declaration:
public class SavingsAccount extends BankAccount
{ ...}
Which of the following statements about these classes is correct?
A.ArrayList is a subclass of ArrayList.
B.ArrayList is a subclass of ArrayList.
C.There is no relationship between ArrayList and ArrayList
D.ArrayList extends ArrayList.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
55
Consider the following code snippet:
public static void reverse(List list) { ...}
Which of the following statements about this code is correct?
A.This code is illegal.
B.This is an example of an unbounded wildcard.
C.This is an example of a wildcard with an upper bound.
D.This is an example of a wildcard with a lower bound.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
56
Which of the following satisfies the wildcard ? super Object?
A.JComponent
B.None satisfy this wildcard.
C.Scanner
D.String
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
57
What does the following code snippet mean:
& Measurable>
A.The class represented by E extends Comparable and Measurable.
B.The class represented by E implements Comparable and extends Measurable.
C.The class represented by E extends Comparable and implements Measurable.
D.The class represented by E implements Comparable and Measurable.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
58
Consider the following code snippet:
public static void fun(T[] t) { ...}
Erasure by the compiler of method fun will generate which result?
A.public static void fun(Object[] t) { ...}
B.public static void fun(Object[] t) { ...}
C.public static void fun(Object t) { ...}
D.public static void fun(Object t) { ...}
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
59
Which code is the equivalent of the following method header?
public static void abc(Stack stack) { ...}
I public static void abc(Stack stack) { ...}
II public static void abc (Stack stack) { ...}
III public static void abc(Stack stack) { ...}
A.II only
B.I only
C.III only
D.I and III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
60
What does it mean when the syntax ? super D is used?
A.Any superclass of D may be used.
B.This indicates a wildcard with a lower bound.
C.Any subclass or superclass of D may be used.
D.Any subclass of D may be used.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
61
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 only
B.I, II and III
C.II only
D.III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
62
Given the following declaration, what is the type parameter in Stack replaced with?
private Stack myStack = new Stack();
A.String
B.Object
C.it is a compiler error
D.int
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
63
Consider the following code snippet:
public class LinkedList
{
private E defaultValue;
public void add(E value, int n) { ...}
private static class Node { public E 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 a static inner class in a generic class as shown.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
64
If a class requires two generic type variables, how many can you legally provide in Java?
I 0
II 1
III 2
A.I and II only
B.I and III only
C.I only
D.II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
65
Consider the following code snippet:
public class LinkedList
{
private E defaultValue;
public static List replicate(E value, int n) { ...}
private class Node { public String data; public Node next;)
...
}
What is wrong with this code?
A.Cannot pass a generic type as an argument to a method.
B.Cannot have a static method in a generic class as shown.
C.Cannot have an inner class in a generic class.
D.Cannot declare the variable defaultValue as a generic type.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
66
Consider the following code snippet:
public interface MyInterface { ...}
public interface YourInterface extends MyInterface { ...}
Which of these are legal class declarations?
I public class SomeClass implements YourInterface { ...}
II public class SomeClass implements YourInterface { ...}
III public class SomeClass implements YourInterface { ...}
A.I and III only
B.III only
C.I only
D.I and II only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
67
Given an array myArray, which of the following represents an expression to determine the type of the elements of the array?
A.myArray.getClass().getComponentType()
B.myArray.getComponentType().getClass()
C.myArray.getClass().getElementType()
D.myArray[0].getClass()
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
68
Suppose a generic method accepts a generic unconstrained argument p.What restrictions are placed on p by the compiler?
A.There are no restrictions.
B.Can only execute methods from its own class.
C.Can only execute Object class methods.
D.Can not execute any methods.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
69
Given an object myObject, which of the following represents the correct code to make a new instance of an object having the same class, assuming that the class has a constructor with no arguments?
A.Object newObject = myObject.newInstance();
B.Object newObject = myObject.newInstance().getClass();
C.Object newObject = myObject.getClass();
D.Object newObject = myObject.getClass().newInstance();
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
70
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.II only
B.I and III only
C.I and II only
D.I only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
71
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.II only
B.III only
C.I only
D.I and III only
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
72
Given the following declaration, what is the type parameter in Stack replaced with?
private Stack myStack = new Stack<>();
A.Object
B.String
C.Stack
D.int
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
73
Consider the following code snippet:
public class LinkedList
{
private static E defaultValue;
public void add(E value, int n) { ...}
private class Node { public String data; public Node next;}
...
}
What is wrong with this code?
A.Cannot pass a generic type as an argument to a method as shown.
B.Cannot have an inner class in a generic class as shown.
C.Cannot declare the variable defaultValue as a generic data type.
D.Cannot declare the variable defaultValue as static.
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
74
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.II only
B.I only
C.I and II only
D.I, II, and III
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
75
Consider the following code snippet:
public static E min(E[] objects)
Which of the following represents the result of type erasure on this code?
A.public static E min(Measurable E[] objects)
B.public static Measurable min(Measurable[] objects)
C.This code is illegal and type erasure will not occur.
D.public static Measurable E min(Measurable E[] objects)
Unlock Deck
Unlock for access to all 75 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 75 flashcards in this deck.