Skip to content
Sharp

Collections · interview question

Given the following code, what is the output? Deque<String> deque = new ArrayDeque<>(); deque.addFirst("A"); deque.addLast("B"); deque.addFirst("C"); System.out.println(deque.pollLast() + deque.pollFirst());?

Multiple choiceHardchapter-4-curatedpremiumcollectionsqueue-and-deque-basics
java
Deque<String> deque = new ArrayDeque<>();
deque.addFirst("A");
deque.addLast("B");
deque.addFirst("C");
System.out.println(deque.pollLast() + deque.pollFirst());
Choose one answer
Answer and explanation

Correct answer

C. BC

Why

The deque ends up in the order [C, A, B]. pollLast() removes 'B' and pollFirst() removes 'C', resulting in the output 'BC'.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →