ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Deque<Integer> d = new ArrayDeque<>();
d.push(1); d.offer(2); d.push(3); d.offerFirst(4); d.add(5);
System.out.print(d.pop() + " " + d.pollLast() + " " + d);
  1. A. 5 4 [3, 1, 2]
  2. B. 4 5 [2, 1, 3]
  3. C. 1 5 [4, 3, 2]
  4. D. 4 5 [3, 1, 2]

Correct answer: D. 4 5 [3, 1, 2]

push/offerFirst add at the head, offer/add at the tail: the deque becomes [4, 3, 1, 2, 5]. pop removes the head (4), pollLast the tail (5), and a deque prints head to tail.

More OOP with C++ and Java questions

Exams that ask this topic