Ten Java data structure programs that do not compile – primitive type arguments, inner node classes, interfaces used as classes, generic arrays, missing returns, comparators and private fields. Find the mistake, then read the fix.
Question 1Easy
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());
}
}
AArrayDeque cannot be used as a stack
BType arguments must be reference types; use Deque<Integer> (autoboxing handles push(1))
Cpush needs an index
Dpop must be assigned to a variable
Correct Answer: Option B
javac: "unexpected type – required: reference, found: int".
The program below does not compile. Which statement correctly describes the problem?
Main.java
public class Main {
class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
System.out.println(head.val + head.next.val);
}
}
ANode needs a no-argument constructor
BNode is a non-static inner class, so it needs an enclosing Main instance; declare it static class Node
Cnext must be initialised to null explicitly
DA class cannot refer to its own type in a field
Correct Answer: Option B
javac: "non-static variable this cannot be referenced from a static context". Node classes almost never need the outer object – make them static.
The program below does not compile. Which statement correctly describes the problem?
Main.java
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> jobs = new Queue<>();
jobs.offer("print report");
System.out.println(jobs.poll());
}
}
Aoffer should be add
BQueue is an interface and cannot be instantiated; use new ArrayDeque<>() or new LinkedList<>()
CQueue must be imported from java.lang
Dpoll requires an argument
Correct Answer: Option B
javac: "Queue is abstract; cannot be instantiated".
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());
}
}
AArrayDeque has no push method
Bpush is declared in Deque, not Queue; declare the variable as Deque<Integer> or use offer
Cpeek is not allowed on a Queue
DInteger 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)".
The program below does not compile. Which statement correctly describes the problem?
Main.java
class ArrayStack<T> {
private T[] items = new T[10];
private int top;
void push(T x) { items[top++] = x; }
T pop() { return items[--top]; }
}
public class Main {
public static void main(String[] args) {
ArrayStack<String> s = new ArrayStack<>();
s.push("x");
System.out.println(s.pop());
}
}
Atop must be initialised to 0
BJava cannot create an array of a type variable (type erasure); use an Object[] (casting on pop) or an ArrayList<T>
CGeneric classes cannot have arrays
Dpush must return T
Correct Answer: Option B
javac: "generic array creation". The usual workaround is Object[] items with @SuppressWarnings("unchecked") T pop() { return (T) items[--top]; }.
The program below does not compile. Which statement correctly describes the problem?
Main.java
public class Main {
static class Node {
int key;
Node left, right;
Node(int key) { this.key = key; }
}
static int height(Node n) {
if (n == null) return 0;
else if (n.left != null || n.right != null) return 1 + Math.max(height(n.left), height(n.right));
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
System.out.println(height(root));
}
}
ARecursion is not allowed in static methods
BA leaf (both children null) reaches the end of the method without a return; javac reports "missing return statement" – the else-if should simply be return 1 + …
CMath.max cannot take method calls
Dheight must be declared before Node
Correct Answer: Option B
Every path through a non-void method must return a value; the compiler does not reason about which conditions are possible.
The program below does not compile. Which statement correctly describes the problem?
Main.java
public class Main {
static class TreeNode {
final int key;
final TreeNode left;
TreeNode right;
TreeNode(int key) { this.key = key; }
}
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
root.right = new TreeNode(20);
System.out.println(root.key + root.right.key);
}
}
Aright must also be final
BThe final field left is never assigned in the constructor; every final field must be definitely assigned – drop final or assign it
Ckey cannot be final
DTreeNode needs a toString method
Correct Answer: Option B
javac: "variable left might not have been initialized". Child links change as a tree grows, so they are normally not final.
The program below does not compile. Which statement correctly describes the problem?
Main.java
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> a > b);
maxHeap.add(3);
maxHeap.add(9);
System.out.println(maxHeap.peek());
}
}
APriorityQueue cannot be a max-heap
BA Comparator must return an int (negative, zero or positive), not a boolean; use (a, b) -> Integer.compare(b, a) or Comparator.reverseOrder()
CLambdas cannot be passed to constructors
Dadd must be offer
Correct Answer: Option B
compare returns int, so the boolean lambda does not match Comparator<Integer> and javac cannot infer the PriorityQueue type.
The program below does not compile. Which statement correctly describes the problem?
Main.java
class LinkedStack {
private static class Node {
int val;
Node next;
Node(int val, Node next) { this.val = val; this.next = next; }
}
private Node top;
private int size;
void push(int v) { top = new Node(v, top); size++; }
}
public class Main {
public static void main(String[] args) {
LinkedStack s = new LinkedStack();
s.push(1);
s.push(2);
System.out.println(s.size);
}
}
ANode must be public
Bsize is private to LinkedStack, a separate top-level class; add a public int size() method
Cpush must return the stack
DLinkedStack needs a constructor
Correct Answer: Option B
javac: "size has private access in LinkedStack". (Private members of a nested class are visible to its enclosing class, which is why the study examples can read them.)
The program below does not compile. Which statement correctly describes the problem?
Main.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, List<Integer>> g = new HashMap<>();
g.computeIfAbsent("A", k -> new ArrayList<>()).add("B");
System.out.println(g);
}
}
AcomputeIfAbsent cannot create lists
BThe neighbour lists are List<Integer>, so add("B") passes a String where an Integer is required; declare Map<String, List<String>>
CHashMap keys must be Integer
Dg must be printed with a loop
Correct Answer: Option B
javac: "incompatible types: String cannot be converted to Integer". The key and neighbour types of a graph are usually the same.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·