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());
}
}[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".
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);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.
Binary Trees and Binary Search Trees · Multiple choice · 1 mark
Inserting the keys in this order into an empty BST gives one particular tree. Which other insertion order produces exactly the same tree?
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.
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);-2
3
2
3In Java % keeps the sign of the dividend, so -17 % 5 is . Math.floorMod rounds the quotient down and returns , in . Math.abs(h) % m is , a valid index but a different bucket from floorMod’s (it sends and to the same bucket, which is legal but not what floorMod does). Adding m to the negative remainder and taking % again gives .
Graphs and Graph Algorithms · Short answer · 2 marks
A maze is an 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 | |
| The number of edges | |
| Running time of the search, as of and |
Answer: ; ;
Each of the rows has horizontal edges and each of the columns has vertical edges. Every cell has at most four neighbours, so and the running time is just : the search does a constant amount of work per cell.
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.
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.
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.