Exam 18: Generic Classes

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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

Free
(Multiple Choice)
4.9/5
(32)
Correct Answer:
Verified

D

Given an array myArray, which of the following represents an expression to determine the type of the elements of the array?

Free
(Multiple Choice)
4.9/5
(37)
Correct Answer:
Verified

A

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<>(); ... box.insert("blue Box"); Double myDouble = (Double) box.getData();

Free
(Multiple Choice)
4.9/5
(35)
Correct Answer:
Verified

D

Consider the following code snippet: public static <T> void fun(T[] t) { ...} Erasure by the compiler of method fun will generate which result?

(Multiple Choice)
4.7/5
(42)

Given the following declaration, what is the type parameter in Stack<E> replaced with? private Stack<String> myStack = new Stack<>();

(Multiple Choice)
4.8/5
(41)

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<>(); box.insert("blue Box");

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.7/5
(33)

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

(Multiple Choice)
4.8/5
(47)

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)

(Multiple Choice)
4.9/5
(48)

Which of the following statements about generic methods is NOT correct?

(Multiple Choice)
4.9/5
(34)

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<>(); II.Box<Boolean> box = new Box<>(); III.Box<double> box = new Box<>();

(Multiple Choice)
4.8/5
(35)

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<? extends E>) { ...} Which type cannot be passed to method locate? i.MyLinkedList<JButton> II.MyLinkedList<Component> III.MyLinkedList<JTextField>

(Multiple Choice)
4.7/5
(37)

Which of the following satisfies the wildcard ? extends Object? i.String II.JComponent III.Scanner

(Multiple Choice)
4.9/5
(32)

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<>(); ... box.insert("blue Box"); String b = box.getData();

(Multiple Choice)
4.9/5
(39)

Which of the following statements about generic methods is correct?

(Multiple Choice)
4.9/5
(29)

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; } }

(Multiple Choice)
4.9/5
(34)

If a class requires two generic type variables, how many can you legally provide in Java? i.0 II.1 III.2

(Multiple Choice)
4.8/5
(32)

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

(Multiple Choice)
4.7/5
(33)

Which of the following satisfies the wildcard ? super Object?

(Multiple Choice)
4.9/5
(37)

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

(Multiple Choice)
4.9/5
(45)
Showing 1 - 20 of 75
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)