Classes, ADTs and Operator Overloading · Multiple choice · 2 marks
class Pair {
public:
Pair(int v) : m_b(v), m_a(m_b + 1) {}
int a() const { return m_a; }
private:
int m_a;
int m_b;
};A client runs Pair p(10);. What is true?
The code does not compile, because m_b is used in the list before it is declared.
p.a() is unpredictable, because m_a is initialised before m_b.
p.a() is 11, because the list initialises m_b first.
p.a() is 1, because every member is zero before the list runs.
Answer: (B)
(B). Members are initialised in the order they are declared in the class, not in the order they appear in the initializer list. m_a is declared first, so it is computed from m_b before m_b has been given any value (an uninitialised int has no guaranteed value). The fix is to declare m_b before m_a, or to write m_a(v + 1).
Dynamic Memory, the Big Three and RAII · Write code · 2 marks
Write findMax. Walk the range with pointers rather than indices.
// REQUIRES: first and last point into the same array, first <= last.
// EFFECTS: returns a pointer to the largest element of [first, last)
// (the leftmost one if several are equal), or nullptr if
// the range is empty.
int* findMax(int* first, int* last) {int* findMax(int* first, int* last) {
if (first == last)
return nullptr;
int* best = first;
for (int* p = first + 1; p != last; ++p) {
if (*p > *best)
best = p;
}
return best;
}Using > rather than >= keeps the leftmost maximum.
Templates, the STL and Iterators · Short answer · 2 marks
struct Point {
int x;
int y;
};
map<Point, string> labels;
labels[Point{1, 2}] = "a";This code does not compile. In one sentence say why, then write one function that fixes it, ordering points by x first and by y when the x values are equal.
A map orders its keys with <, and Point has no operator<.
bool operator<(const Point& a, const Point& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}(1 mark for the reason, 1 mark for a correct comparison that handles equal x.)
Stacks and Queues · Predict the output · 2 marks
What does this code print? Assume the needed headers are included and using namespace std;.
queue<int> q;
for (int i = 1; i <= 5; i++)
q.push(i * i);
stack<int> s;
while (q.size() > 2) {
s.push(q.front());
q.pop();
}
while (!s.empty()) {
q.push(s.top());
s.pop();
}
while (!q.empty()) {
cout << q.front() << "\n";
q.pop();
}16
25
9
4
1The queue starts as 1, 4, 9, 16, 25. The first loop moves 1, 4, 9 onto the stack (stopping when two remain), so 9 is on top. The second loop appends 9, 4, 1 behind 16, 25.
Binary Trees and Binary Search Trees · Find the bug · 2 marks
This function should return whether key is stored in the binary search tree rooted at root. It sometimes crashes.
bool contains(const Node* root, int key) {
const Node* p = root;
while (p->key != key) {
if (key < p->key) {
p = p->left;
} else {
p = p->right;
}
}
return true;
}Give two different inputs for which it has undefined behaviour. [1]
Rewrite lines 3–10 so that the function is correct. [1]
An empty tree (root == nullptr, so p->key dereferences a null pointer), and any key that is not in the tree (the search walks off a leaf and then dereferences nullptr).
while (p != nullptr) {
if (key == p->key) return true;
if (key < p->key) p = p->left;
else p = p->right;
}
return false;Programming and Introductory Data Structures · University of Michigan (Michigan, UMich, U of M) · also written EECS280
Make a free, printable EECS 280 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 Michigan library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh EECS 280 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 Michigan. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.