Java OCA OCP Practice Question 2705

Question

Consider the following program:

import java.util.*;

class Main {//from ww  w  .  j a  v a  2s  .c  o  m
       public static void main(String []args) {
               Deque<String> deque = new ArrayDeque<String>(2);
               deque.addFirst("one ");
               deque.addFirst("two ");
               deque.addFirst("three ");
               System.out.print(deque.pollLast());
               System.out.print(deque.pollLast());
               System.out.print(deque.pollLast());
       }
}

What does this program print when executed?

  • a) one two three
  • b) three two one
  • c) one one one
  • d) three three three


a)

Note

The addFirst() inserts an element in the front of the Deque object and pollLast() method retrieves and removes the element from the other end of the object.

Since the elements "one," "two," and "three" are inserted in one end and retrieved in another end, the elements are retrieved in the same order as they were inserted.




PreviousNext

Related