LearnFrenzy
Sign in Take a free test

Data Structures in Java · Exercise 3 of 4

Data Structures in Java Data Structures in Java – Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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 1 Easy

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))
C push needs an index
D pop must be assigned to a variable
Question page & discussion
Question 2 Medium

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);
    }
}
A Node needs a no-argument constructor
B Node is a non-static inner class, so it needs an enclosing Main instance; declare it static class Node
C next must be initialised to null explicitly
D A class cannot refer to its own type in a field
Question page & discussion
Question 3 Easy

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());
    }
}
A offer should be add
B Queue is an interface and cannot be instantiated; use new ArrayDeque<>() or new LinkedList<>()
C Queue must be imported from java.lang
D poll requires an argument
Question page & discussion
Question 4 Medium

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
C peek is not allowed on a Queue
D Integer cannot be stored in a Queue
Question page & discussion
Question 5 Medium

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());
    }
}
A top must be initialised to 0
B Java cannot create an array of a type variable (type erasure); use an Object[] (casting on pop) or an ArrayList<T>
C Generic classes cannot have arrays
D push must return T
Question page & discussion
Question 6 Medium

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));
    }
}
A Recursion is not allowed in static methods
B A 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 + …
C Math.max cannot take method calls
D height must be declared before Node
Question page & discussion
Question 7 Medium

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);
    }
}
A right must also be final
B The final field left is never assigned in the constructor; every final field must be definitely assigned – drop final or assign it
C key cannot be final
D TreeNode needs a toString method
Question page & discussion
Question 8 Medium

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());
    }
}
A PriorityQueue cannot be a max-heap
B A Comparator must return an int (negative, zero or positive), not a boolean; use (a, b) -> Integer.compare(b, a) or Comparator.reverseOrder()
C Lambdas cannot be passed to constructors
D add must be offer
Question page & discussion
Question 9 Easy

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);
    }
}
A Node must be public
B size is private to LinkedStack, a separate top-level class; add a public int size() method
C push must return the stack
D LinkedStack needs a constructor
Question page & discussion
Question 10 Easy

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);
    }
}
A computeIfAbsent cannot create lists
B The neighbour lists are List<Integer>, so add("B") passes a String where an Integer is required; declare Map<String, List<String>>
C HashMap keys must be Integer
D g must be printed with a loop
Question page & discussion
Up next · Exercise 4Data Structures in Java Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links