Sample COMPSCI 61B questions
  1. Interfaces, Generics and Dynamic Dispatch · Predict the output · 2 marks

    What does the program print? Write each line of output on its own line.

    class Vehicle {
        String describe() { return "vehicle"; }
        String label() { return "[" + describe() + "]"; }
    }
    class Car extends Vehicle {
        String describe() { return "car"; }
    }
    class Taxi extends Car {
        String describe() { return "taxi " + super.describe(); }
    }
    public class Main {
        public static void main(String[] args) {
            Vehicle v = new Taxi();
            System.out.println(v.label());
            System.out.println(new Car().label());
            System.out.println(new Vehicle().label());
        }
    }
    Show solution
    [taxi car]
    [car]
    [vehicle]

    label is inherited from Vehicle and calls describe() on this, so the override of the actual object runs. In a Taxi, super.describe() is the Car version, which returns "car".

  2. ArrayLists, Linked Lists and Deques · Predict the output · 3 marks

    What does the following code print? Write each line of output on its own line.

    List<Integer> xs = new ArrayList<>(List.of(3, 8, 5, 6, 7));
    Iterator<Integer> it = xs.iterator();
    int seen = 0;
    while (it.hasNext()) {
        int x = it.next();
        seen += x;
        if (x % 2 == 0 || seen > 20) {
            it.remove();
        }
        System.out.println(x + " " + xs.size());
    }
    System.out.println(xs);
    Show solution
    3 5
    8 4
    5 4
    6 3
    7 2
    [3, 5]

    The running total seen is 3, 11, 16, 22, 29. The elements 8 and 6 are removed because they are even, and 7 is removed because seen has passed 20; the iterator’s remove() is legal each time because next() was just called.

  3. Binary Trees and Binary Search Trees · Multiple choice · 1 mark

    Inserting the keys 50,30,70,20,4050, 30, 70, 20, 40 in this order into an empty BST gives one particular tree. Which other insertion order produces exactly the same tree?

    1. 30,50,70,20,4030, 50, 70, 20, 40

    2. 50,70,30,40,2050, 70, 30, 40, 20

    3. 50,20,30,70,4050, 20, 30, 70, 40

    4. 40,30,20,50,7040, 30, 20, 50, 70

    Show solution

    Answer: (B)

    (B). The tree has root 50, children 30 and 70, and below 30 the children 20 and 40. A tree is reproduced by any order in which every node is inserted after its parent. In (B), 70 and 30 both come right after 50, and 40 and 20 come after 30. In (A) and (D) a different key becomes the root. In (C) the key 20 is inserted before 30 and becomes the left child of 50, so 30 ends up as the right child of 20.

  4. Hash Tables, hashCode and equals · Predict the output · 2 marks

    What does the following code print? Write each line of output on its own line.

    int h = -17;
    int m = 5;
    System.out.println(h % m);
    System.out.println(Math.floorMod(h, m));
    System.out.println(Math.abs(h) % m);
    System.out.println((h % m + m) % m);
    Show solution
    -2
    3
    2
    3

    In Java % keeps the sign of the dividend, so -17 % 5 is −2-2. Math.floorMod rounds the quotient down and returns 33, in 0..40..4. Math.abs(h) % m is 17 mod 5=217 \bmod 5 = 2, a valid index but a different bucket from floorMod’s (it sends −17-17 and 1717 to the same bucket, which is legal but not what floorMod does). Adding m to the negative remainder and taking % again gives 33.

  5. Graphs and Graph Algorithms · Short answer · 2 marks

    A maze is an R×CR \times C grid in which every cell is open. Each cell is a vertex, and two cells are joined by an (undirected) edge when they share a side. Breadth-first search from the top-left cell visits the cells without building any adjacency list: it computes the neighbours of a cell from its row and column.

    The number of vertices VV   
    The number of edges EE   
    Running time of the search, as Θ\Theta of RR and CC   
    Show solution

    Answer: RCRC; R(C−1)+(R−1)CR(C-1) + (R-1)C; Θ(RC)\Theta(RC)

    Each of the RR rows has C−1C - 1 horizontal edges and each of the CC columns has R−1R - 1 vertical edges. Every cell has at most four neighbours, so E≤2VE \le 2V and the running time Θ(V+E)\Theta(V + E) is just Θ(RC)\Theta(RC): the search does a constant amount of work per cell.

About COMPSCI 61B practice exams

COMPSCI 61B practice exams

Data Structures · University of California, Berkeley (Berkeley, UC Berkeley, Cal) · also written COMPSCI61B

Make a free, printable COMPSCI 61B practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 550 original questions and skips the ones you have already seen.

Looking for COMPSCI 61B past exams?

Start with the past exams and tests your instructor or the Berkeley library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPSCI 61B practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.

Topics covered

  • Interfaces, Generics and Dynamic Dispatch · 50 questions
  • Asymptotic Analysis of Java Code · 50 questions
  • ArrayLists, Linked Lists and Deques · 50 questions
  • Stacks, Queues and Their Uses · 50 questions
  • Recursion and Backtracking · 50 questions
  • Binary Trees and Binary Search Trees · 50 questions
  • Balanced Search Trees · 50 questions
  • Hash Tables, hashCode and equals · 50 questions
  • Priority Queues and Heaps · 50 questions
  • Sorting Algorithms · 50 questions
  • Graphs and Graph Algorithms · 50 questions

More Berkeley courses

questionbank.dev is independent and not affiliated with University of California, Berkeley. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.