Linked Lists · Multiple choice · 2 marks
Node* mystery(Node* h) {
if (h == nullptr || h->next == nullptr)
return h;
Node* second = h->next;
h->next = mystery(second->next);
second->next = h;
return second;
}head points to the list . What is the list that mystery(head) returns?
Answer: (A)
(A). The function swaps each pair of neighbours and leaves a final unpaired node where it is. mystery(1,2,3,4,5) makes 2 the new front; 1 then points to the result of mystery(3,4,5), which is , because that call swaps 3 and 4 and then hits the base case at the lone 5.
Recursion · Short answer · 2 marks
The function below is intended for n >= 1, and the caller never passes a smaller value on purpose.
int fact(int n) {
if (n == 1) return 1;
return n * fact(n - 1);
}A user calls fact(0). What happens, and why? What one-character change to the condition makes the function safe for every n?
The argument goes and never equals , so the base case is never reached: the recursion does not stop and eventually overflows the call stack (the program crashes). Changing the condition to n <= 1 fixes it; the base case now covers every argument that cannot be reduced further.
Dynamic Memory, the Big Three and RAII · Multiple choice · 2 marks
The class Buf defines a constructor Buf(int n), a copy constructor, a copy assignment operator and a destructor. Consider this code, with line numbers.
void show(Buf b);
Buf a(3);
Buf b = a;
Buf c(a);
Buf d(2);
d = a;
show(a);On which lines is the copy constructor called?
Lines 4, 5 and 8
Lines 4 and 5 only
Lines 4, 5, 7 and 8
Lines 4, 5, 6 and 8
Answer: (A)
(A) Line 4 initialises a new object from an existing one, so it is a copy construction even though it uses =. Line 5 is the same. Line 7 assigns to an existing object, which calls the copy assignment operator. Line 8 passes a by value, so the parameter b is copy-constructed. Lines 3 and 6 call Buf(int).
Binary Trees and Binary Search Trees · Short answer · 2 marks
A binary search tree contains the keys 8, 3, 1, 6, 4, 7, 10, 14, 13, and its preorder traversal is exactly this sequence. If the root (8) is deleted, which two keys could take its place so that the result is still a binary search tree? Say which is the predecessor and which the successor.
7 and 10. The preorder says 3, 1, 6, 4, 7 form the left subtree (all smaller than 8) and 10, 14, 13 the right subtree. The inorder predecessor is the largest key in the left subtree, 7 (go to 3, then right to 6, then right to 7); the inorder successor is the smallest key in the right subtree, 10 (the right child itself, as it has no left child).
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).
Software Design and Implementation I · The University of Texas at Austin (UT Austin, UT, UTexas) · also written ECE312
Make a free, printable ECE 312 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 450 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh ECE 312 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 The University of Texas at Austin. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.