Weekly Quizzes — W13 to W16

10 questions each · ~15 min each · attempt ALL ten before opening the key · ← W09–W12 · Hub


W13 — Pointers & dynamic memory (Week 13)

Q1. [Easy] int* p = &n; makes p hold…

Q2. [Easy] *p means…

Q3. [Easy] Which pointer value means “points at nothing”?

Q4. [Medium] What does this print?

int x = 5;
int* p = &x;
*p = 12;
cout << x;

Q5. [Medium] What does this print?

int a[3] = {7, 8, 9};
int* p = a;
cout << *(p + 2);

Q6. [Medium] int* data = new int[10]; must eventually be matched by…

Q7. [Medium] What does this function return?

int* makeValue() {
    int v = 42;
    return &v;
}

Q8. [Medium] A memory leak happens when…

Q9. [Hard] What does this print?

void setTo(int* p) { *p = 99; }
int main() {
    int v = 1;
    setTo(&v);
    cout << v;
}

Q10. [Hard] After delete[] data; the best immediate practice is…

W13 — Answer key

Q1 — b. & is the address-of operator; a pointer is an address stored in a variable.

Q2 — b. * in an expression is the dereference: follow the address, get the value. (In a declaration the same character means “is a pointer” — context decides.)

Q3 — b. nullptr is the modern, type-safe null pointer. It compares false and cannot accidentally convert into an int the way 0 could.

Q4 — b. *p = 12 writes through the address into x itself. This is how functions modify caller data without references.

Q5 — c. p + 2 advances two ints, not two bytes → &a[2] → 9. Pointer arithmetic is scaled by element size.

Q6 — b. new[]/delete[] are the paired forms. Mismatching them (delete on an array) is undefined behavior.

Q7 — b. The local’s lifetime ends at the closing brace; the returned address points at a dead object. It may look to work — undefined behavior’s silence is the danger.

Q8 — b. The allocation is unreachable — no delete, no pointer left — so it is lost to the program until exit. Leaks accumulate; long programs die of them.

Q9 — b. The pointer parameter receives &v; *p = 99 writes into v. Same effect as the reference version from Week 8, older syntax.

Q10 — b. A dangling pointer reused is silent corruption; a null pointer reused is a loud crash you can fix in seconds. Null-after-delete is cheap insurance.


W14 — Structs & classes: introducing OOP (Week 14)

Q1. [Easy] A struct groups…

Q2. [Easy] Given struct Student { string name; int marks; }; and Student s;, you read the marks with…

Q3. [Easy] The default access of members in a struct is…

Q4. [Medium] What does this print?

struct Point { int x; int y; };
int main() {
    Point p = {3, 4};
    cout << p.x + p.y;
}

Q5. [Medium] What does this print?

struct P { int v; };
void bump(P q) { q.v++; }
int main() {
    P p = {5};
    bump(p);
    cout << p.v;
}

Q6. [Medium] Which constructor declaration is correct for class Box with members int w, h;?

Q7. [Medium] What does this print?

class Counter {
    int v;
public:
    Counter() : v(0) {}
    void inc() { v++; }
    int get() const { return v; }
};
int main() {
    Counter c;
    c.inc(); c.inc(); c.inc();
    cout << c.get();
}

Q8. [Medium] Why make data members private and expose methods?

Q9. [Hard] What does this print?

class Tracer {
public:
    Tracer()  { cout << "B"; }
    ~Tracer() { cout << "D"; }
};
int main() {
    Tracer t;
    cout << "|";
}

Q10. [Hard] A class invariant — “balance is never negative” — is best enforced by…

W14 — Answer key

Q1 — b. The struct is the course’s first record: name, marks, price — related fields traveling together under one type.

Q2 — a. Dot for objects; -> is for pointers to objects (Week 13 meets this: p->marks ≡ (*p).marks).

Q3 — b. Struct defaults to public; class to private. That single default is the only language difference — the course uses struct for open records, class for encapsulated state.

Q4 — b. Brace-initialization fills members in declaration order: x=3, y=4 → 7.

Q5 — b. Structs pass by value like any other argument. P& q would have made it 6 — the Week-8 lesson applying to records now.

Q6 — a. Constructors share the class name, declare no return type, and the member-initializer list is the idiomatic body. (b) wrongly “returns” int; (c) wrongly declares void.

Q7 — b. Each inc() increments the object’s own v. Methods read and write members directly — the implicit “this object” is the point of a method.

Q8 — b. Private data means every change passes through your methods — where validation lives. Public data means anyone can set balance to −999.

Q9 — b. The constructor runs at declaration (B), the pipe prints, and at main’s closing brace the destructor runs (D). Object lifetime is scope-driven — the RAII seed.

Q10 — b. Invariants live in the type: private state + validating mutations. This is precisely A-38’s cannot-exist-invalid design from the practice bank.


W15 — Applied OOP (Week 15)

Q1. [Easy] A member function differs from a free function because it…

Q2. [Easy] this inside a member function refers to…

Q3. [Easy] A const member function promises…

Q4. [Medium] What does this print?

class Acc {
    long long bal = 0;
public:
    Acc& add(long long amt) { bal += amt; return *this; }
    long long get() const { return bal; }
};
int main() {
    Acc a;
    a.add(50).add(20);
    cout << a.get();
}

Q5. [Medium] What does this print?

class Box {
    int v;
public:
    Box(int v) : v(v) {}
    int get() const { return v; }
};
void take(const Box& b) { cout << b.get(); }
int main() {
    Box b(9);
    take(b);
}

Q6. [Medium] Composition means…

Q7. [Medium] Which is the correct operator<< signature for printing class Item?

Q8. [Medium] What does this print?

class P {
public:
    P()  { cout << "c"; }
    ~P() { cout << "d"; }
};
int main() {
    P a;
    {
        P b;
        cout << "|";
    }
    cout << "!";
}

Q9. [Hard] What does this print?

class V {
    vector<int> data;
public:
    void add(int x) { data.push_back(x); }
    int sum() const {
        int s = 0;
        for (int x : data) s += x;
        return s;
    }
};
int main() {
    V v;
    v.add(3); v.add(4); v.add(5);
    cout << v.sum();
}

Q10. [Hard] The inheritance preview showed class Dog : public Animal. The one-sentence division of labor is…

W15 — Answer key

Q1 — b. Member functions are invoked through an object (obj.fn()) and see that object’s members — the implicit parameter behind this.

Q2 — b. this is the address of the object the method was called on; members are implicitly accessed through it.

Q3 — b. The trailing const on a method forbids member modification — and is required before a const object may call the method (Q5’s partner fact).

Q4 — b. add returns *this by reference, so a.add(50) evaluates to a, and .add(20) chains. Fluent interfaces are this trick plus taste.

Q5 — a. Two facts interlock: const& avoids the copy, and the const get() is exactly what a const reference is allowed to call. This pairing is the course’s default parameter shape.

Q6 — b. Has-a: a Car has-an Engine as a member. Composition is the default relationship; inheritance (is-a) must earn its place.

Q7 — a. Stream operators are free functions taking the stream by reference (left) and the object by const& (right), returning the stream for chaining. Everything else mis-arranges those pieces.

Q8 — a. a constructs (c), b constructs inside the block (c would make option c’s output — count again: the inner c prints before the pipe), b dies at the inner brace (d), then !. Sequence: c, c, |, d, ! — wait. The output is cc|d!? No: a’s c, b’s c, |, b’s d, ! → cc|d!. The listed option (a) shows c|d! — if you answered (a), re-trace: two constructors run before the pipe. The honest verdict: none of the four options is exact, and the correct trace is cc|d!. Mark yourself correct only for that trace.

Q9 — a. 3+4+5 = 12. A class wrapping a vector — the standard shape of the week’s labs; the range-for works identically inside a member function.

Q10 — b. Is-a: Dog inherits Animal’s public interface (and protected state), adds its own, and may override. Copying code (a) is duplication; containment (c) is composition — a different relationship.

Correction note for Q8: the option list above contains an error — the true trace is cc|d!. Treat “none of the above” as the fifth, correct answer; the trace itself is the lesson.


W16 — Capstone & the modern toolkit (Week 16)

Q1. [Easy] The capstone’s first design artifact is…

Q2. [Easy] auto x = 3.5; makes x…

Q3. [Easy] Which replaces owning raw pointers in modern course code?

Q4. [Medium] What does this print?

for (auto x : {2, 4, 6}) cout << x;

Q5. [Medium] constexpr int SIZE = 10; int a[SIZE]; — the difference from const is…

Q6. [Medium] What does this print?

auto add = [](int a, int b) { return a + b; };
cout << add(2, 3);

Q7. [Medium] RAII means…

Q8. [Medium] What does this print?

vector<int> v = {1, 2, 3};
for (int& x : v) x *= 10;
for (int x : v) cout << x;

Q9. [Hard] A capstone menu keeps working after bad input. The boundary that saves it is…

Q10. [Hard] The modern-toolkit habit that most directly kills the leak family from Week 13 is…

W16 — Answer key

Q1 — b. The domain classes and their invariants are the program’s skeleton; menus and files hang off them. Design precedes interface.

Q2 — b. auto deduces from the initializer — here double. It is not a dynamic type; the variable is as typed as ever, just spelled once.

Q3 — b. vector for growable arrays, unique_ptr for single-owner objects — cleanup rides on destructors, and the leak family loses its habitat.

Q4 — a. Range-for over a brace list; no separators are printed because none were written. Output formatting is always explicit.

Q5 — b. constexpr promises compile-time evaluability — required for array bounds in strict usage, and it documents intent: this is a constant, not a read-only runtime value.

Q6 — a. A lambda is an anonymous function object; calling it with (2, 3) returns 5. The course’s rule: lambdas for short, local, call-site logic.

Q7 — a. Acquisition Is Initialization: the resource’s lifetime is bound to an object’s scope. Destructors — Week 15’s B/D tracer — become the cleanup engine.

Q8 — a. The int& makes the range variable a reference, so x *= 10 writes back: 10, 20, 30. Drop the & and you modify copies — 123.

Q9 — a. Failed input leaves the stream failed: clear() resets the state, the bad characters must be discarded (ignore to end-of-line), then re-prompt. Robust menus are this three-step dance in a loop.

Q10 — b. Raw new/delete are where leaks, double-frees, and dangling pointers live. Containers and smart pointers move ownership into the type system — the modern course’s closing argument.


← W09–W12 · Topic tests → · Hub