Sample ECSE 250 questions
  1. 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;
        }
    }
    1. Give two Money objects a and b with a.cents > b.cents for which a.compareTo(b) returns a negative number. [2]

    2. Rewrite compareTo so that it is correct for all values. [1]

    Show solution
    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.

    2. return Integer.compare(cents, other.cents);

    Subtraction is only safe when the operands are known to be small. Integer.compare returns −1-1, 0 or 1 without computing a difference.

  2. Asymptotic Analysis of Java Code · Multiple choice · 1 mark

    To show that f(n)=3n2+10nf(n) = 3n^2 + 10n is in O(n2)O(n^2) we need constants cc and n0n_0 with f(n)≤c n2f(n) \le c\,n^2 for every n≥n0n \ge n_0. Which pair of constants works?

    1. c=3c = 3 and n0=1n_0 = 1

    2. c=4c = 4 and n0=5n_0 = 5

    3. c=4c = 4 and n0=10n_0 = 10

    4. c=3c = 3 and n0=1000n_0 = 1000

    Show solution

    Answer: (C)

    (C). 3n2+10n≤4n23n^2 + 10n \le 4n^2 exactly when 10n≤n210n \le n^2, that is when n≥10n \ge 10. With n0=5n_0 = 5 the inequality fails at n=5n = 5 (125>100125 > 100). With c=3c = 3 it fails for every nn, because 3n2+10n>3n23n^2 + 10n > 3n^2, however large n0n_0 is.

  3. 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.

    1. List the array indices that hold the items, from the front of the queue to the back.     [1]

    2. At which index does the next call to offer store its item?     [1]

    Show solution

    Answer: 6, 7, 0, 1, 2; 3

    The items occupy (front + i) % 8 for i=0,…,4i = 0, \ldots, 4, which is 6, 7, 0, 1, 2 (the queue wraps around the end of the array). The next item goes at (front + size) % 8 = 11 mod 8=311 \bmod 8 = 3.

  4. 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.)

    1. 4

    2. 3

    3. 5

    4. 10

    Show solution

    Answer: (A)

    (A). A tree of height hh has at most 2h+1−12^{h+1} - 1 nodes. For h=3h = 3 that is only 15, which is too few, and for h=4h = 4 it is 31, which is enough (a complete tree with 20 nodes has height 4). Taking ⌈log⁡220⌉=5\lceil \log_2 20 \rceil = 5 forgets that the root alone is already height 0, and 10 is 20/220/2, which has no meaning here.

  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 ECSE 250 practice exams

ECSE 250 practice exams

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.

Looking for ECSE 250 past exams?

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.

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
  • Sorting Algorithms · 50 questions
  • Graphs and Graph Algorithms · 50 questions

More McGill courses

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.