Exam 18: Generic Classes

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

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

(Multiple Choice)
4.9/5
(38)

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?

(Multiple Choice)
4.8/5
(29)

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

(Multiple Choice)
4.8/5
(31)

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.

(Multiple Choice)
4.9/5
(50)

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

(Multiple Choice)
4.7/5
(39)

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();

(Multiple Choice)
5.0/5
(38)

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?

(Multiple Choice)
4.9/5
(39)

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>

(Multiple Choice)
4.9/5
(37)

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

(Multiple Choice)
4.9/5
(37)

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>

(Multiple Choice)
4.9/5
(40)

Consider the following class declaration: public class SavingsAccount extends BankAccount { . . . } Which of the following statements about these classes is correct?

(Multiple Choice)
4.8/5
(37)

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

(Multiple Choice)
4.9/5
(36)

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

(Multiple Choice)
4.8/5
(31)

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?

(Multiple Choice)
4.8/5
(37)

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();

(Multiple Choice)
4.9/5
(32)

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

(Multiple Choice)
4.8/5
(39)

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.

(Multiple Choice)
4.8/5
(37)

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

(Multiple Choice)
4.9/5
(26)

Consider the following code snippet: public static void reverse(List<?> list) { . . . } This method declaration is equivalent to which of the following declarations?

(Multiple Choice)
4.9/5
(34)

Consider the following code snippet: ArrayList<Double> arr = new ArrayList<Double>(); String element = arr.get(0); Is there an error in this code?

(Multiple Choice)
4.9/5
(35)
Showing 21 - 40 of 75
close modal

Filters

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