Sample ECE244H1 questions
  1. Classes, ADTs and Operator Overloading · Multiple choice · 2 marks

    class Rational {
    public:
        Rational(int n = 0, int d = 1);          // not explicit
        Rational operator+(const Rational& rhs) const;
        // ...
    };
    
    Rational a(1, 2), b(3, 4);

    Exactly one of the following expressions does not compile. Which one?

    1. 2 + a

    2. a + 2

    3. a + b + a

    4. a + Rational(2)

    Show solution

    Answer: (A)

    (A). a + 2 means a.operator+(2), and the argument 2 is converted to a Rational by the non-explicit constructor. For 2 + a there is no a on the left, so no member function can be called, and the compiler does not convert the left operand of a member operator. Making operator+ a non-member function would let both 2 + a and a + 2 compile.

  2. Dynamic Memory, the Big Three and RAII · Write code · 4 marks

    A table of ints is stored as an array of rows pointers, each pointing to its own array of cols ints.

    // REQUIRES: rows >= 1 and cols >= 1
    // EFFECTS:  returns a new table with every entry 0.
    int** makeTable(int rows, int cols) {
    
    // REQUIRES: t came from makeTable(rows, ...) and was not freed.
    // EFFECTS:  frees every block that makeTable allocated.
    void freeTable(int** t, int rows) {

    Write both functions.

    Show solution
    int** makeTable(int rows, int cols) {
        int** t = new int*[rows];
        for (int r = 0; r < rows; r++)
            t[r] = new int[cols]();
        return t;
    }
    
    void freeTable(int** t, int rows) {
        for (int r = 0; r < rows; r++)
            delete[] t[r];
        delete[] t;
    }

    There are rows + 1 blocks. The row arrays must be freed first: once t itself is deleted the row pointers are gone. Marks: 2 for makeTable (spine and rows), 2 for freeTable (rows, then spine).

  3. Linked Lists · Write code · 4 marks

    struct Node {
        int value;
        Node* next;
    };

    Write dedupe. In a list whose values are sorted in ascending order, it deletes every node whose value equals the value of the node before it, so each value appears once. It returns the head. No node may leak and no node may be deleted twice. For example, 1,1,2,2,2,31,1,2,2,2,3 becomes 1,2,31,2,3.

    // REQUIRES: the values of head's list are in ascending order
    // MODIFIES: the list
    // EFFECTS: deletes nodes so that no two neighbours are equal
    Node* dedupe(Node* head) {
    Show solution
    Node* dedupe(Node* head) {
        Node* p = head;
        while (p != nullptr && p->next != nullptr) {
            if (p->next->value == p->value) {
                Node* dead = p->next;
                p->next = dead->next;
                delete dead;
            } else {
                p = p->next;
            }
        }
        return head;
    }

    The first node is never removed, so head does not change. When the next node is a duplicate we unlink and delete it but stay on p, because the node after it may be a duplicate as well; we advance only when the next node differs. (1 mark guard against nullptr, 1 mark compare with next, 1 mark unlink and delete, 1 mark stay on p after a delete.)

  4. Binary Trees and Binary Search Trees · Predict the output · 3 marks

    What does this program print? Assume the needed headers and using namespace std;.

    struct Node {
        int key;
        Node* left;
        Node* right;
    };
    int mystery(const Node* r, int depth) {
        if (r == nullptr) return 0;
        return r->key * depth
             + mystery(r->left, depth + 1)
             + mystery(r->right, depth + 1);
    }
    int main() {
        Node a{2, nullptr, nullptr};
        Node b{4, nullptr, nullptr};
        Node c{1, nullptr, nullptr};
        Node d{5, &a, &b};
        Node e{7, nullptr, &c};
        Node f{3, &d, &e};
        cout << mystery(&f, 1) << "\n";
        cout << mystery(&e, 0) << "\n";
        cout << mystery(nullptr, 5) << "\n";
    }
    Show solution
    48
    1
    0

    mystery(&f, 1) sums key times depth, with the root at depth 1: 3⋅1+5⋅2+2⋅3+4⋅3+7⋅2+1⋅3=483\cdot1 + 5\cdot2 + 2\cdot3 + 4\cdot3 + 7\cdot2 + 1\cdot3 = 48. Starting node 7 at depth 0 gives 7⋅0+1⋅1=17\cdot0 + 1\cdot1 = 1, and the empty tree gives 0.

  5. Big-O and Sorting · Short answer · 2 marks

    A booking system must sort 10610^6 bookings by date. The total time must be O(nlog⁡n)O(n \log n) even on adversarial input, and bookings with the same date must stay in their arrival order. Choose one algorithm from the table and say why each of the other three fails.

    AlgorithmWorst caseAverageExtra memoryStable
    Insertion sortn2n^2n2n^2O(1)O(1)yes
    Selection sortn2n^2n2n^2O(1)O(1)no
    Quicksort, last-element pivotn2n^2nlog⁡nn \log nO(log⁡n)O(\log n)no
    Merge sortnlog⁡nn \log nnlog⁡nn \log nO(n)O(n)yes
    Show solution

    Merge sort: it is the only row with worst case nlog⁡nn \log n and it is stable; the price is O(n)O(n) extra memory. Insertion sort is stable but Θ(n2)\Theta(n^2) in the worst case, which is hopeless for 10610^6 items (about 101210^{12} steps). Selection sort is n2n^2 on every input and not stable. Quicksort is not stable and an adversarial (for example already sorted) input makes it n2n^2.

About ECE244H1 practice exams

ECE244H1 practice exams

Programming Fundamentals · University of Toronto (UofT, U of T, UToronto) · also written ECE244, ECE 244

Make a free, printable ECE244H1 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.

Looking for ECE244H1 past exams?

Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ECE244H1 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

  • Classes, ADTs and Operator Overloading · 50 questions
  • Dynamic Memory, the Big Three and RAII · 50 questions
  • Inheritance and Virtual Functions · 50 questions
  • Linked Lists · 50 questions
  • Binary Trees and Binary Search Trees · 50 questions
  • Hash Tables · 50 questions
  • Big-O and Sorting · 50 questions

More UofT courses

questionbank.dev is independent and not affiliated with University of Toronto. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.