Java OCA OCP Practice Question 1585

Question

What is the output of the following?

5:   ArrayDeque<Integer> d = new ArrayDeque<>();
6:   d.offer(18);
7:   d.offer(5);
8:   d.push(13);
9:   System.out.println(d.poll() + " " + d.poll());
  • A. 13 18
  • B. 18 5
  • C. 18 13
  • D. None of the above


A.

Note

The offer() method adds an element to the back of the queue.

After line 7 completes, the queue contains 18 and 5 in that order.

The push() method adds an element to the front of the queue.

The element 13 pushes past everyone on the line.

After line 8 completes, the queue now contains 13, 18, and 5, in that order.

Then we get the first two elements from the front, which are 13 and 18, making Option A correct.




PreviousNext

Related