LearnFrenzy
Sign in Take a free test

Data Structures in Java · Data Structures in Java – Find the Error · Question 1

Error: a stack declared as Deque<int> Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

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

public class Main {
    public static void main(String[] args) {
        Deque<int> stack = new ArrayDeque<>();
        stack.push(1);
        System.out.println(stack.pop());
    }
}
  • A ArrayDeque cannot be used as a stack
  • B Type arguments must be reference types; use Deque<Integer> (autoboxing handles push(1)) Correct
  • C push needs an index
  • D pop must be assigned to a variable
Correct Answer: Option B

javac: "unexpected type – required: reference, found: int".

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