LearnFrenzy
Sign in Take a free test

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

Error: calling push through a Queue reference Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

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

Main.java
import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<Integer> q = new ArrayDeque<>();
        q.push(10);
        q.push(20);
        System.out.println(q.peek());
    }
}
  • A ArrayDeque has no push method
  • B push is declared in Deque, not Queue; declare the variable as Deque<Integer> or use offer Correct
  • C peek is not allowed on a Queue
  • D Integer cannot be stored in a Queue
Correct Answer: Option B

The compiler only allows the methods of the declared type. javac: "cannot find symbol – method push(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