Interfaces, Generics and Dynamic Dispatch · Multiple choice · 1 mark
The class below defines equals using only the field id.
class Item {
final int id;
final String label;
Item(int id, String label) { this.id = id; this.label = label; }
@Override
public boolean equals(Object o) {
return o instanceof Item i && id == i.id;
}
// hashCode() goes here
}Which implementation of hashCode violates the contract between equals and hashCode?
return Objects.hash(id, label);
return (id * 31) % 1024;
return 17;
return Integer.hashCode(id) + 1;
Answer: (A)
(A). The contract says that objects that are equal must have equal hash codes. Here two items with the same id and different labels are equal, but Objects.hash(id, label) gives them different hash codes. The other three depend only on id (a constant such as 17 is legal, only slow), so equal objects always agree.
ArrayLists, Linked Lists and Deques · Multiple choice · 1 mark
What does the following code print?
List<StringBuilder> a = new ArrayList<>();
a.add(new StringBuilder("x"));
a.add(new StringBuilder("y"));
List<StringBuilder> b = new ArrayList<>(a);
b.get(0).append("!");
b.add(new StringBuilder("z"));
b.set(1, new StringBuilder("w"));
System.out.println(a);[x, y]
[x!, w]
[x!, y]
[x!, w, z]
Answer: (C)
(C). The copy constructor makes a new list with its own slots, but the slots hold the same StringBuilder objects as a (a shallow copy). b.get(0).append("!") changes the shared object, so a shows x!. Adding to b and replacing b’s slot 1 change only b’s own slots, so a still has two elements and keeps y.
Recursion and Backtracking · Write code · 2 marks
Complete the method below using recursion and no loop.
/** Return the largest of a[i], a[i + 1], ..., a[a.length - 1].
* Precondition: 0 <= i < a.length.
* Example: for a = {3, 9, 4}, maxFrom(a, 0) returns 9
* and maxFrom(a, 2) returns 4. */
static int maxFrom(int[] a, int i) {static int maxFrom(int[] a, int i) {
if (i == a.length - 1) {
return a[i];
}
return Math.max(a[i], maxFrom(a, i + 1));
}The base case is the last element, because the precondition rules out an empty range. Otherwise the answer is the larger of a[i] and the maximum of the rest.
Hash Tables, hashCode and equals · Predict the output · 2 marks
String.hashCode() computes from the character codes of a string of length ; the empty string gives 0. The codes are 'A' , 'B' , 'a' and 'b' . What does the following code print? Write each line of output on its own line.
System.out.println("ab".hashCode());
System.out.println("Aa".hashCode() == "BB".hashCode());
System.out.println("Aa".equals("BB"));
System.out.println("".hashCode());3105
true
false
0"ab" gives . "Aa" gives and "BB" gives , so the hash codes collide although the strings differ.
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 · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS314
Make a free, printable C S 314 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 500 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh C S 314 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 The University of Texas at Austin. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.