Java OCA OCP Practice Question 3284

Question

Consider the following program and predict the output:

import java.util.*;

public class Main {
     public static void main(String []args) {
             Deque<Integer> deque = new LinkedList<>();
             deque.add(10);/*from  w ww. j  av  a 2 s  .com*/
             deque.add(20);
             deque.peek();
             deque.peek();
             deque.peek(); //#1
             System.out.println(deque);
     }
}
  • a) [10, 20]
  • b) [20, 10]
  • c) []
  • d) NoSuchElementException thrown when executing the line #1.


a)

Note

The method peek() retrieves an element from the head of the Deque and returns, but does not remove the element.

Hence, there will be no impact on the Deque.




PreviousNext

Related