Classes, ADTs and Operator Overloading · Predict the output · 3 marks
What does this program print? Assume <iostream> is included and using namespace std;.
class Cell {
public:
Cell() {
m_id = ++s_made;
cout << "make " << m_id << "\n";
}
int id() const { return m_id; }
static int made() { return s_made; }
private:
int m_id;
static int s_made;
};
int Cell::s_made = 0;
int main() {
Cell a;
Cell arr[3];
Cell b = arr[1];
cout << a.id() << b.id() << " " << Cell::made() << "\n";
Cell c;
cout << c.id() << "\n";
}make 1
make 2
make 3
make 4
13 4
make 5
5a is cell 1 and the three array elements are 2, 3 and 4. Cell b = arr[1]; uses the compiler’s copy constructor, which copies m_id (3) and does not run the user-written constructor, so nothing is printed and s_made stays at 4. The next new cell is number 5.
Templates, the STL and Iterators · Multiple choice · 1 mark
A student wants to keep Card objects in a set<Card>.
struct Card {
int rank;
char suit;
};Which single addition is enough to make set<Card> s; s.insert(Card{5, 'h'}); compile and behave as a set ordered by rank?
bool operator<(const Card& a, const Card& b) { return a.rank < b.rank; }
bool operator==(const Card& a, const Card& b) { return a.rank == b.rank; }
bool operator>(const Card& a, const Card& b) { return a.rank > b.rank; }
ostream& operator<<(ostream& os, const Card& c) { return os << c.rank; }
Answer: (A)
(A). A set orders and compares its elements using only < (two elements are “equal” when neither is less than the other). ==, > and << are never consulted, so adding them does not help.
Stacks and Queues · Find the bug · 2 marks
This function should remove every occurrence of x from q and keep the other elements in their original order, using only the queue operations.
void removeAll(queue<int>& q, int x) {
for (int i = 0; i < q.size(); i++) {
int v = q.front();
q.pop();
if (v != x)
q.push(v);
}
}What is q (front first) after removeAll(q, 1) when q initially holds 1, 2, 1, 3? [1]
Explain the bug and rewrite the first line of the loop so the function is correct. [1]
3, 2. The loop stops after three iterations instead of four, with the 3 and 2 in the wrong order.
q.size() is re-evaluated every iteration and shrinks whenever an element is discarded, so the loop ends before every original element has been examined (and the queue has not been rotated fully). Fix: int n = q.size(); before the loop and for (int i = 0; i < n; i++).
Binary Trees and Binary Search Trees · Predict the output · 2 marks
What does this program print? Assume the needed headers and using namespace std;.
struct Node {
int key;
Node* left;
Node* right;
};
void show(const Node* root) {
if (root == nullptr) return;
cout << "[" << root->key;
show(root->left);
show(root->right);
cout << "]";
}
int main() {
Node e{50, nullptr, nullptr};
Node d{40, nullptr, nullptr};
Node c{30, nullptr, nullptr};
Node b{20, &d, &e};
Node a{10, &b, &c};
show(&a);
cout << "\n";
}[10[20[40][50]][30]]Each call prints [, its key, the output of its left subtree, then of its right subtree, then ]; so every node’s brackets enclose its subtrees.
Big-O and Sorting · Predict the output · 2 marks
What does this program print? Assume the needed headers and using namespace std;. Within each sort, no two words tie.
bool byLength(const string& a, const string& b) {
return a.size() < b.size();
}
bool byLastLetter(const string& a, const string& b) {
return a.back() < b.back();
}
void show(const vector<string>& w) {
int n = w.size();
for (int i = 0; i < n; i++)
cout << w[i] << (i + 1 < n ? " " : "\n");
}
int main() {
vector<string> w = {"cherry", "fig", "bananas", "kiwi", "apple"};
sort(w.begin(), w.end(), byLength);
show(w);
sort(w.begin(), w.end(), byLastLetter);
show(w);
}fig kiwi apple cherry bananas
apple fig kiwi bananas cherryThe first sort orders by length 3, 4, 5, 6, 7. The second sort ignores the first order completely and orders by last letter: e (apple), g (fig), i (kiwi), s (bananas), y (cherry).
Data Structures and Algorithms · McMaster University (McMaster, Mac, MacU) · also written COMPENG2SI3
Make a free, printable COMPENG 2SI3 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 McMaster library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPENG 2SI3 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 McMaster University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.