Interfaces, Generics and Dynamic Dispatch · Find the bug · 3 marks
A sorted list of Money values sometimes comes out in the wrong order.
class Money implements Comparable<Money> {
final int cents;
Money(int cents) { this.cents = cents; }
public int compareTo(Money other) {
return cents - other.cents;
}
}Give two Money objects a and b with a.cents > b.cents for which a.compareTo(b) returns a negative number. [2]
Rewrite compareTo so that it is correct for all values. [1]
For example a.cents = 2000000000 and b.cents = -2000000000. The difference should be 4000000000, which does not fit in an int (largest value 2147483647), so it overflows to the negative number -294967296 and a looks smaller than b.
return Integer.compare(cents, other.cents);
Subtraction is only safe when the operands are known to be small. Integer.compare returns , 0 or 1 without computing a difference.
Asymptotic Analysis of Java Code · Multiple choice · 1 mark
To show that is in we need constants and with for every . Which pair of constants works?
and
and
and
and
Answer: (C)
(C). exactly when , that is when . With the inequality fails at (). With it fails for every , because , however large is.
Stacks, Queues and Their Uses · Short answer · 2 marks
A circular queue stores its items in an array of length 8. Its fields are front = 6 and size = 5, and the item at the front is at index front.
List the array indices that hold the items, from the front of the queue to the back. [1]
At which index does the next call to offer store its item? [1]
Answer: 6, 7, 0, 1, 2; 3
The items occupy (front + i) % 8 for , which is 6, 7, 0, 1, 2 (the queue wraps around the end of the array). The next item goes at (front + size) % 8 = .
Binary Trees and Binary Search Trees · Multiple choice · 1 mark
What is the smallest possible height of a binary tree with 20 nodes? (The height of a single node is 0.)
4
3
5
10
Answer: (A)
(A). A tree of height has at most nodes. For that is only 15, which is too few, and for it is 31, which is enough (a complete tree with 20 nodes has height 4). Taking forgets that the root alone is already height 0, and 10 is , which has no meaning here.
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.
Fundamentals of Software Development · McGill University (McGill, McGill U) · also written ECSE250
Make a free, printable ECSE 250 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 400 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the McGill library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ECSE 250 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 McGill University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.