Interfaces, Generics and Dynamic Dispatch · Multiple choice · 1 mark
What happens when the following code is compiled?
interface Greeter {
String greet(String name);
}
class Polite implements Greeter {
String greet(String name) { return "Dear " + name; }
}It compiles; greet is simply package-private in Polite.
Compile error: Polite does not implement greet and is not abstract.
It compiles, but calling greet on a Greeter variable fails at run time.
Compile error: greet needs to be public in Polite, as in Greeter.
Answer: (D)
(D). Every method of an interface is implicitly public, and an overriding method may not have weaker access than the method it overrides. javac reports “attempting to assign weaker access privileges; was public”. The tempting second answer is wrong because the method is implemented, only with the wrong access modifier; the compiler never lets an access error slip through to run time.
Asymptotic Analysis of Java Code · Find the bug · 2 marks
The method below processes the tasks of an ArrayList in order and leaves the list empty. It gives the right answer but becomes very slow for long lists.
static long drain(List<Integer> tasks) {
long total = 0;
while (!tasks.isEmpty()) {
total += tasks.remove(0);
}
return total;
}Give the running time in terms of = tasks.size(), and explain in one sentence where the time goes. [1]
Rewrite the method so it takes time and still leaves tasks empty. [1]
. remove(0) on an ArrayList shifts every remaining item one place left, , , … moves in total.
static long drain(List<Integer> tasks) {
long total = 0;
for (int t : tasks) {
total += t;
}
tasks.clear();
return total;
}Read the items in order in one pass, then clear() the list once, which is . (Removing from the end of an ArrayList is , but would process the tasks in reverse order.)
Balanced Search Trees · Write code · 1 mark
The class TreeNode is on the reference sheet, and the methods rotateLeft(TreeNode x) and rotateRight(TreeNode x) have been written already: each rotates the subtree rooted at x and returns the new root of that subtree. Complete the method below using them.
/** Perform a left-right double rotation at x and return the new
* root of the subtree.
* Precondition: x.left and x.left.right are not null.
* Example: for 30 whose left child 10 has the right child 20,
* returns the node 20, with children 10 and 30. */
static TreeNode rotateLeftRight(TreeNode x) {static TreeNode rotateLeftRight(TreeNode x) {
x.left = rotateLeft(x.left);
return rotateRight(x);
}The first rotation turns the zig-zag below x into a straight line and must be stored in x.left, because x.left is no longer the root of that subtree. The second rotation then lifts the new left child above x.
Hash Tables, hashCode and equals · Multiple choice · 1 mark
Each of the four classes below has two int fields x and y and defines equals and hashCode as described. Which class breaks the contract between equals and hashCode?
equals compares x and y; hashCode returns x
equals compares x and y; hashCode returns 31 * x + y
equals compares x and y; hashCode returns 7
equals compares only x; hashCode returns 31 * x + y
Answer: (D)
(D). The contract says equal objects must have equal hash codes. In (D), (1, 2) and (1, 5) are equal (same x) but hash to 33 and 36. (A) and (C) are legal: they collide often (every object in (C) lands in one bucket, so it is slow) but equal objects always agree on the hash code. (B) is the usual correct pair.
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 and Algorithms · University of Ottawa (uOttawa, U of O, UOttawa) · also written CSI2110
Make a free, printable CSI 2110 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 350 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the uOttawa library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CSI 2110 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 Ottawa. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.