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?
2 + a
a + 2
a + b + a
a + Rational(2)
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.
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.
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).
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, becomes .
// 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) {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.)
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";
}48
1
0mystery(&f, 1) sums key times depth, with the root at depth 1: . Starting node 7 at depth 0 gives , and the empty tree gives 0.
Big-O and Sorting · Short answer · 2 marks
A booking system must sort bookings by date. The total time must be 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.
| Algorithm | Worst case | Average | Extra memory | Stable |
| Insertion sort | yes | |||
| Selection sort | no | |||
| Quicksort, last-element pivot | no | |||
| Merge sort | yes |
Merge sort: it is the only row with worst case and it is stable; the price is extra memory. Insertion sort is stable but in the worst case, which is hopeless for items (about steps). Selection sort is on every input and not stable. Quicksort is not stable and an adversarial (for example already sorted) input makes it .
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.
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.
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.