LearnFrenzy
Sign in Take a free test

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

A sequence of push and pop on ArrayDeque Answer, explanation & discussion

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

What is the output of the following Java program?

Main.java
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        Deque<Integer> st = new ArrayDeque<>();
        st.push(1); st.push(2); st.push(3);
        st.pop();
        st.push(4); st.push(5);
        st.pop();
        System.out.println(st.peek() + " " + st);
    }
}
  • A 4 [4, 2, 1] Correct
  • B 4 [1, 2, 4]
  • C 5 [5, 4, 2, 1]
  • D 2 [4, 2, 1]
Correct Answer: Option A

push adds at the head and pop removes from the head, so toString lists the stack from top to bottom.

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