ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
Deque<Integer> d = new ArrayDeque<>();
d.push(1);
d.push(2);
d.offer(3);
d.addFirst(4);
System.out.print(d.pop() + "" + d.pollLast() + d.peek());
  1. A. 432
  2. B. 412
  3. C. 234
  4. D. 431

Correct answer: A. 432

push and addFirst insert at the head and offer at the tail, giving [4, 2, 1, 3]. pop removes the head (4), pollLast the tail (3), and peek then sees 2.

More OOP with C++ and Java questions

Exams that ask this topic