Sample COMP 250 questions
  1. Interfaces, Generics and Dynamic Dispatch · Write code · 2 marks

    Write a generic class Pair<A, B> that stores two values of possibly different types. It has two final fields first (of type A) and second (of type B), a constructor taking both values, and a method swap() that returns a new Pair<B, A> with the two values exchanged.

    Show solution
    class Pair<A, B> {
        final A first;
        final B second;
    
        Pair(A first, B second) {
            this.first = first;
            this.second = second;
        }
    
        Pair<B, A> swap() {
            return new Pair<>(second, first);
        }
    }

    The return type shows that the type arguments are exchanged as well as the values.

  2. ArrayLists, Linked Lists and Deques · Write code · 3 marks

    The class Node<T> from the reference sheet is used for singly linked lists. Complete the method below. It must not use any method of java.util.

    /** Add a new node holding v at the end of the list starting at
     *  head, and return the head of the resulting list. The list may
     *  be empty.
     *  Example: for 1 -> 2 and v = 3, returns the head of 1 -> 2 -> 3. */
    static Node<Integer> append(Node<Integer> head, int v) {
    Show solution
    static Node<Integer> append(Node<Integer> head, int v) {
        Node<Integer> node = new Node<>(v, null);
        if (head == null) {
            return node;
        }
        Node<Integer> c = head;
        while (c.next != null) {
            c = c.next;
        }
        c.next = node;
        return head;
    }

    For an empty list the new node is the new head. Otherwise we stop at the last node (the one whose next is null), not after it, so that we still have a node to attach the new one to.

  3. Recursion and Backtracking · Predict the output · 2 marks

    What does the program print? Write each line of output on its own line.

    public class Main {
        static void walk(int lo, int hi) {
            if (lo > hi) {
                return;
            }
            int mid = (lo + hi) / 2;
            System.out.println(mid);
            walk(lo, mid - 1);
            walk(mid + 1, hi);
        }
    
        public static void main(String[] args) {
            walk(1, 6);
        }
    }
    Show solution
    3
    1
    2
    5
    4
    6

    walk(1, 6) prints 3, then handles the left range 1..2 completely (it prints 1, then 2) before it starts the right range 4..6 (it prints 5, then 4, then 6).

  4. Hash Tables, hashCode and equals · Write code · 3 marks

    Complete the method. It must run in O(n)O(n) expected time, where n=n = a.length.

    /** Return {i, j} with i < j and a[i] + a[j] == target, or null
     *  if there are no such indices.
     *  Precondition: at most one pair of indices works.
     *  Example: for {2, 7, 11, 15} and target 9, returns {0, 1}. */
    static int[] twoSum(int[] a, int target) {
    Show solution
    static int[] twoSum(int[] a, int target) {
        Map<Integer, Integer> index = new HashMap<>();
        for (int j = 0; j < a.length; j++) {
            Integer i = index.get(target - a[j]);
            if (i != null) {
                return new int[] {i, j};
            }
            index.put(a[j], j);
        }
        return null;
    }

    The map holds the index of every earlier value. For each a[j] the partner target - a[j] is looked up before a[j] itself is added, so an element is never paired with itself, and i < j holds automatically. A repeated value works: for {3, 3} and target 6 the second 3 finds the first.

  5. Graphs and Graph Algorithms · Short answer · 2 marks

    A directed acyclic graph on the vertices 0,1,2,3,40, 1, 2, 3, 4 has exactly the edges 0→20 \to 2, 1→21 \to 2 and 2→32 \to 3. How many different topological orders does it have? Show the reasoning in a sentence or two.

    Show solution

    10. Vertices 0 and 1 must both come before 2, and 2 before 3, so the vertices 0,1,2,30, 1, 2, 3 appear as either 0,1,2,30, 1, 2, 3 or 1,0,2,31, 0, 2, 3: two orders. Vertex 4 is unconstrained and can be inserted into any of the five gaps of that sequence, so there are 2⋅5=102 \cdot 5 = 10 topological orders.

About COMP 250 practice exams

COMP 250 practice exams

Introduction to Computer Science · McGill University (McGill, McGill U) · also written COMP250

Make a free, printable COMP 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 500 original questions and skips the ones you have already seen.

Looking for COMP 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 COMP 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
  • Hash Tables, hashCode and equals · 50 questions
  • Priority Queues and Heaps · 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.