Question 2
Back to Exercise
What is the output of the following Java program?
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> q = new ArrayDeque<>();
q.offer("A"); q.offer("B"); q.offer("C");
q.poll();
q.offer("D");
q.offer(q.poll());
System.out.println(q);
}
}
- A [B, C, D]
- B [C, D, B] Correct
- C [D, C, B]
- D [C, B, D]
Correct Answer: Option B
A leaves, D joins, then B is polled from the front and offered again at the back.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!