ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
Queue<Integer> q = new ArrayDeque<>();
q.add(1); q.add(2); q.offer(3);
q.poll();
System.out.print(q.peek() + " " + q.size());
  1. A. 2 2
  2. B. 3 2
  3. C. 1 3
  4. D. 2 3

Correct answer: A. 2 2

As a Queue, ArrayDeque is FIFO: add/offer append at the tail, poll removes the head (1), peek shows the new head (2) without removing it.

More OOP with C++ and Java questions

Exams that ask this topic