Question 1
Back to Exercise
What is the output of the following Java program?
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!