LearnFrenzy
Sign in Take a free test

Data Structures in Java · Data Structures in Java Output Questions · Question 2

Rotating the front of a queue to the back Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links