Java OCA OCP Practice Question 2725

Question

Which options are true of the following code? (Choose all that apply.)

3:    ______________<Integer> q = new LinkedList<>(); 
4:    q.add(10); 
5:    q.add(12); 
6:    q.remove(1); 
7:    System.out.print(q); 
  • A. If we fill in the blank with List, the output is [10].
  • B. If we fill in the blank with List, the output is [10, 12].
  • C. If we fill in the blank with Queue, the output is [10].
  • D. If we fill in the blank with Queue, the output is [10, 12].
  • E. The code does not compile in either scenario.
  • F. A runtime exception is thrown.


A, D.

Note

A LinkedList implements both List and Queue.

The List interface has a method to remove by index.

Since this method exists, Java does not autobox to call the other method.

Queue has only the remove by object method, so Java does autobox there.

Since the number 1 is not in the list, Java does not remove anything for the Queue.




PreviousNext

Related