Cumulative Tests — CU3 and CU4

15 questions each · ~30–35 min each · CU3 assumes Weeks 1–13, CU4 assumes the whole course · attempt ALL before any key · ← CU1–CU2 · Practice finals →


CU3 — Everything through pointers & dynamic memory (Weeks 1–13)

Q1. [Easy] Which correctly swaps two ints through a function?

Q2. [Easy] string s = to_string(42) + "!"; gives…

Q3. [Easy] Reading a file line by line uses…

Q4. [Medium] What does this print?

void addTax(double& price) { price *= 1.15; }
int main() {
    double p = 200;
    addTax(p);
    cout << p;
}

Q5. [Medium] What does this print?

int a[4] = {1, 2, 3, 4};
int* p = a + 3;
cout << *p;

Q6. [Medium] What does this print?

ifstream in("data.txt");
if (!in) { cout << "missing"; return 0; }
cout << "open";

(data.txt does not exist)

Q7. [Medium] What does this print?

int x = 10;
int* p = &x;
int& r = x;
r = 20;
cout << *p;

Q8. [Medium] This code leaks. Where?

void process(int n) {
    int* data = new int[n];
    if (n == 0) return;          // early exit!
    for (int i = 0; i < n; i++) data[i] = i;
    cout << data[n - 1];
    delete[] data;
}

Q9. [Medium] What does this print?

string line = "Ali,88";
size_t comma = line.find(',');
cout << line.substr(comma + 1);

Q10. [Hard] What does this print?

int arr[5] = {5, 10, 15, 20, 25};
int* p = arr;
p += 2;
cout << *p << " " << *(p - 1);

Q11. [Hard] What does this print?

void fill(int* a, int n) {
    for (int i = 0; i < n; i++) a[i] = i * i;
}
int main() {
    int* data = new int[4];
    fill(data, 4);
    cout << data[3];
    delete[] data;
}

Q12. [Hard] What does this print?

int* makePair() {
    int* p = new int[2];
    p[0] = 7; p[1] = 8;
    return p;                    // heap memory survives the return
}
int main() {
    int* q = makePair();
    cout << q[0] << q[1];
    delete[] q;
}

Q13. [Hard] A program reads an unknown count of values from a file into a collection, then reports the median. The best container choice is…

Q14. [Hard] What does this print?

string names[3] = {"Zoe", "Ali", "Mia"};
for (int i = 0; i < 3; i++)
    for (int j = i + 1; j < 3; j++)
        if (names[j] < names[i]) {
            string t = names[i]; names[i] = names[j]; names[j] = t;
        }
cout << names[0];

Q15. [Hard] The dangling-pointer return from Week 13 (return &v; for a local v) sometimes prints the right value. The engineering lesson is…

CU3 — Answer key

Q1 — b. References let the swap touch the caller’s variables. (a) swaps copies — the null op that feels like work.

Q2 — b. to_string is the number→string bridge; + concatenates.

Q3 — b. getline loops on stream truth — line grain, newline consumed.

Q4 — b. double& aliases p: 200 × 1.15 = 230. In/out through a reference.

Q5 — b. a+3 is &a[3] → 4. Arithmetic scaled by element size, from the decayed array start.

Q6 — b. The failed open sets the fail state; !in is true → missing. The guard converts a silent disaster into a handled event.

Q7 — b. Three names, one object: r is x is *p. Writing through any alias, reading through any other.

Q8 — b. The early return abandons data with no delete — the leak hides on the exceptional path, which is exactly where leaks live. (Fixes: delete before return, or restructure so cleanup is automatic — the RAII lesson.)

Q9 — b. find locates the comma; substr(comma+1) is everything after — 88. The CSV slice.

Q10 — a. += 2 moves two ints forward (index 2 → 15); −1 back one (index 1 → 10). Bidirectional pointer arithmetic.

Q11 — b. The array decays; fill writes squares through it; data[3] is

  1. Pointer + length crossing the boundary — Week 9’s convention with Week 13’s memory.

Q12 — b. Heap memory outlives its creating function — that is why heap exists. The returned pointer is valid until delete; ownership transferred to the caller. (Compare: returning &local — stack — was the bug.)

Q13 — b. Unknown count at compile time → dynamic container. vector manages its own growth; the alternative manual new/realloc dance is the Week-13 discipline exercise, not the default answer.

Q14 — a. Selection sort with string < — lexicographic: Ali, Mia, Zoe. names[0] = Ali. Strings order themselves; the swap moves whole strings.

Q15 — b. “Appears to work” is undefined behavior’s most dangerous feature. The fix list — return the value, extend the lifetime, or move to the heap with ownership — is the vocabulary the whole memory module exists to teach.


CU4 — The full course (Weeks 1–16)

Q1. [Easy] Which prints 3.14 from double pi = 3.14159;?

Q2. [Easy] struct Student { string name; int marks; }; — which copies one student’s marks into another existing object?

Q3. [Easy] Which is the const-correct read-only parameter?

Q4. [Medium] What does this print?

class Acc {
    int bal = 0;
public:
    void deposit(int a) { if (a > 0) bal += a; }
    int get() const { return bal; }
};
int main() {
    Acc a;
    a.deposit(100); a.deposit(-5);
    cout << a.get();
}

Q5. [Medium] What does this print?

vector<int> v = {8, 3, 8, 1};
int count = 0;
for (int x : v) if (x == 8) count++;
cout << count;

Q6. [Medium] What does this print?

int a[5] = {9, 2, 7, 1, 5};
// selection sort pass 1: move the minimum to index 0
int mi = 0;
for (int i = 1; i < 5; i++) if (a[i] < a[mi]) mi = i;
int t = a[0]; a[0] = a[mi]; a[mi] = t;
cout << a[0] << a[4];

Q7. [Medium] What does this print?

string s = "abc";
s += "def";
cout << s.size();

Q8. [Medium] What does this print?

class Box {
public:
    int v;
    Box(int v) : v(v) {}
};
void take(Box b) { b.v = 99; }
int main() {
    Box b(1);
    take(b);
    cout << b.v;
}

Q9. [Medium] A file holds 5\n10\n15\n. What does this print?

ifstream in("n.txt");
int x, sum = 0;
while (in >> x) sum += x;
cout << sum;

Q10. [Hard] What does this print?

int fib(int n) {
    if (n <= 1) return n;
    return fib(n - 1) + fib(n - 2);
}
int main() {
    cout << fib(5);
}

Q11. [Hard] What does this print?

map<string, int> stock;   // #include <map>
stock["pen"] = 4;
stock["pen"] += 2;
cout << stock["pen"];

Q12. [Hard] What does this print?

try {
    int marks = -1;
    if (marks < 0) throw invalid_argument("negative");
    cout << "ok";
} catch (const invalid_argument& e) {
    cout << "caught";
}

Q13. [Hard] What does this print?

class Base {
public:
    virtual string who() const { return "base"; }
};
class Derived : public Base {
public:
    string who() const { return "derived"; }
};
void report(const Base& b) { cout << b.who(); }
int main() {
    Derived d;
    report(d);
}

Q14. [Hard] What does this print?

int* data = new int[3]{1, 2, 3};
int* alias = data;
delete[] data;
data = nullptr;
cout << (alias == nullptr);

Q15. [Hard] The capstone stores Student objects in a vector, saves them to a text file, reloads at startup, and rejects any marks outside 0–100 at both entry points (user input and file load). The architectural principle this enacts is…

CU4 — Answer key

Q1 — b. fixed + setprecision(2) — the formatting pair, from <iomanip>.

Q2 — b. Member-to-member assignment between existing objects. (a) overwrites b entirely with an int — a type error.

Q3 — b. const&: no copy, read-only enforced. The signature the course adopts for every read-only string parameter from Week 8 onward.

Q4 — a. The class refuses its own corruption: deposit(−5) fails the guard. The invariant lives in the type — Q4 of T8, matured into the capstone.

Q5 — b. 8 appears at indices 0 and 2 → count 2. Linear counting over a vector — the ==-test pattern.

Q6 — a. Pass 1 finds min index 3 (value 1), swaps with slot 0: {1,2,7,9,5}. a[0]=1, a[4]=5 → 15. One selection pass, traced.

Q7 — b. += appends; 3+3 = 6 characters. size() counts what the string is, not what it was.

Q8 — b. take receives a copy of the Box; the copy’s v became 99 and died at the closing brace. Objects follow value semantics unless passed by reference — the Week-8 lesson wearing a class.

Q9 — a. Three extractions across three lines: 5+10+15 = 30. >> treats newlines as ordinary whitespace.

Q10 — a. fib(5) = fib(4)+fib(3) = 3+2 = 5. Double recursion — the tree the algorithms module traces. The base cases n ≤ 1 return n itself (0 and 1), anchoring the sequence.

Q11 — b. First use creates the key with 4; += updates it to 6. The map’s insert-or-access bracket — a side effect to use deliberately.

Q12 — b. throw abandons the normal path instantly; the catch handler prints caught. “ok” never runs — control left that line the moment the throw fired.

Q13 — b. A Base& holding a Derived object calls the Derived override — runtime polymorphism through the virtual function. The reference’s static type is Base; the object’s dynamic type is Derived, and virtual makes the call follow the object.

Q14 — b. Nulling data did not touch alias — two independent pointers held one address; the delete killed the memory, not the copies. Alias is dangling regardless of data’s new value. Null-after-delete protects that pointer only — one more reason owning containers beat manual pointers.

Q15 — a. One doorway: every mark — typed or loaded — passes the same 0–100 gate before entering the system. Duplicate validation (b) drifts out of sync; trusting the file (c) is how corrupt data enters via your own hand. The single-path principle is the capstone’s quiet spine.


← CU1–CU2 · Final Exam A → · Hub