Cumulative Tests — CU1 and CU2

15 questions each · ~30 min each · everything so far is fair game — cumulative means the early material returns, mixed and combined · attempt ALL before any key · Hub · CU3–CU4 →


CU1 — Everything through loops (Weeks 1–6)

Q1. [Easy] The command that compiles lab.cpp with the course’s standard flags is…

Q2. [Easy] What does cout << 17 % 5; print?

Q3. [Easy] Which reads a whole line with spaces?

Q4. [Easy] if (x = 5) — what is wrong?

Q5. [Medium] What does this print?

int x = 10;
if (x > 5) cout << "big";
if (x > 8) cout << "bigger";
if (x > 12) cout << "biggest";

Q6. [Medium] What does this print?

int total = 0;
for (int i = 1; i <= 4; i++)
    total += i;
cout << total;

Q7. [Medium] What does this print?

int n = 6;
while (n > 0) {
    cout << n % 2;
    n /= 2;
}

Q8. [Medium] What does this print?

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++)
        cout << i << j << " ";
}

Q9. [Medium] This loop should read numbers until 0, but has a bug:

int x, sum = 0;
while (x != 0) {
    cin >> x;
    sum += x;
}
cout << sum;

The bug is…

Q10. [Medium] What does this print?

int balance = 1000;
int withdraw = 300;
if (withdraw > 0 && withdraw <= balance)
    cout << "ok";
else
    cout << "denied";

Q11. [Hard] What does this print?

int n = 4729;
int rev = 0;
while (n > 0) {
    rev = rev * 10 + n % 10;
    n /= 10;
}
cout << rev;

Q12. [Hard] What does this print?

for (int row = 1; row <= 3; row++) {
    for (int s = 0; s < 3 - row; s++) cout << " ";
    for (int star = 0; star < 2 * row - 1; star++) cout << "*";
    cout << "\n";
}

Q13. [Hard] A program must keep asking for a mark until it lands in 0–100. The correct loop is…

Q14. [Hard] What does this print?

int count = 0;
for (int i = 10; i <= 99; i++) {
    if (i % 7 == 0) count++;
}
cout << count;

Q15. [Hard] The single most important habit when a loop misbehaves is…

CU1 — Answer key

Q1 — b. The standard invocation: standard, warnings, source, -o name.

Q2 — b. 17 = 3·5 + 2 → remainder 2.

Q3 — b. getline reads the whole line; >> stops at whitespace.

Q4 — b. The classic = vs == slip: assignment succeeds (x becomes 5), its value 5 is truthy, the branch fires. Compilers warn; the habit is reading warnings as errors.

Q5 — b. Two independent ifs: 10 > 5 and 10 > 8 both true, 10 > 12 false. An else-if ladder would print only big.

Q6 — b. 1+2+3+4 = 10. The accumulator against a bounded for.

Q7 — a. Parity print + halve: 6%2=0 (n→3), 3%2=1 (n→1), 1%2=1 (n→0). Output 011 — 6 in binary is 110, printed in reverse. Binary decomposition is the digit-peel loop with base 2.

Q8 — a. Outer i runs 0–2, inner j runs 0–1 per pass, completing fully each time — row-major order.

Q9 — b. Two defects: the first test reads an uninitialized x (undefined), and even with luck, the 0 gets added to sum. The clean form tests the read itself: while (cin >> x && x != 0).

Q10 — a. 300 > 0 and 300 ≤ 1000 → both sides true → ok. The && guard is the withdrawal rule the bank labs enforce.

Q11 — b. The peel-and-rebuild: 9, then 7, then 2, then 4 → 9274. Reverse by arithmetic.

Q12 — a. Padding 3−row spaces then 2·row−1 stars per row — the centered pyramid. Dependent bounds both matter: one shapes position, the other width.

Q13 — a. Do-while fits: the first read must happen, then repetition continues exactly while the value is invalid. (b) tests m before it exists; (c) reads 100 times; (d) reads once.

Q14 — b. From 14 to 98 step 7: (98−14)/7 + 1 = 13. Counting multiples in a range by the endpoints — the closed form behind the loop.

Q15 — b. The trace table is the course’s universal loop debugger — slower than hope, correct more often. Output (c) is a fine supplement, but the table is what finds logic errors.


CU2 — Everything through collections (Weeks 1–9)

Q1. [Easy] Which is a valid array declaration with initialization?

Q2. [Easy] vector<int> v; — which reports how many elements it holds?

Q3. [Easy] A function receiving int a[] also receives int n because…

Q4. [Medium] What does this print?

int a[5] = {9, 4, 7, 1, 8};
int m = a[0];
for (int i = 1; i < 5; i++)
    if (a[i] < m) m = a[i];
cout << m;

Q5. [Medium] What does this print?

vector<int> v = {3, 6, 9};
for (int x : v) cout << x * 2;

Q6. [Medium] What does this print?

int a[4] = {5, 10, 15, 20};
cout << a[1] + a[3];

Q7. [Medium] What does this print?

vector<int> v;
for (int i = 0; i < 4; i++) v.push_back(i + 10);
cout << v.size() << ":" << v[2];

Q8. [Medium] What does this print?

int a[6] = {1, 2, 3, 4, 5, 6};
int count = 0;
for (int i = 0; i < 6; i++)
    if (a[i] % 3 == 0) count++;
cout << count;

Q9. [Medium] A marks array must report how many students scored above the average. The minimal structure is…

Q10. [Medium] What does this print?

double avg(int a[], int n) {
    long long s = 0;
    for (int i = 0; i < n; i++) s += a[i];
    return (double)s / n;
}
int main() {
    int a[3] = {7, 8, 9};
    cout << avg(a, 3);
}

Q11. [Hard] What does this print?

vector<int> v = {4, 1, 7, 2};
int best = 0;
for (size_t i = 1; i < v.size(); i++)
    if (v[i] > v[best]) best = i;
cout << best << "@" << v[best];

Q12. [Hard] What does this print?

int a[5] = {3, 1, 4, 1, 5};
for (int i = 0; i < 4; i++)
    if (a[i] > a[i + 1]) {
        int t = a[i]; a[i] = a[i + 1]; a[i + 1] = t;
    }
for (int i = 0; i < 5; i++) cout << a[i];

Q13. [Hard] What does this print?

int a[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int s = 0;
for (int i = 0; i < 3; i++) s += a[i][i];
cout << s;

Q14. [Hard] What does this print?

vector<int> v = {5, 3, 8};
v[1] = v[0] + v[2];
for (size_t i = 0; i < v.size(); i++) cout << v[i];

Q15. [Hard] A student’s marks analyzer prints the max but crashes for empty input. The missing guard is…

CU2 — Answer key

Q1 — a. Braced initialization fills elements in order.

Q2 — b. size() is the vector’s element count. sizeof returns bytes of the vector object, not its contents.

Q3 — b. Decay is the reason. The length parameter is the API convention built on that fact.

Q4 — b. Min scan — the max pattern with <. Seeding from a[0] keeps it correct for all-negative data.

Q5 — b. Range-for binds copies by default: x doubles for output, the vector’s elements untouched. int& x would have doubled the vector.

Q6 — b. a[1] = 10, a[3] = 20 → 30. Index arithmetic before sentiment.

Q7 — a. Pushed 10, 11, 12, 13 → size 4; index 2 is 12.

Q8 — b. 3 and 6 pass the % 3 test → 2.

Q9 — b. The average is unknown until the sum is — pass 1 computes it, pass 2 compares. Two passes exist because the data is stored; a streaming version cannot know the average mid-stream.

Q10 — b. The function returns 8.0; default cout formatting prints 8. Formatting is presentation — the value is what the function owns.

Q11 — a. best walks to index 2 (value 7); output pairs position@value. The index-as-artifact pattern.

Q12 — a. One bubble pass compares (3,1)→swap, (3,4)→ok, (4,1)→swap, (4,5)→ok → 1 3 1 4 5. One pass does not sort — it bubbles. The full sort needs n−1 passes; seeing one pass proves you understand the mechanism, not the slogan.

Q13 — a. The i == j identity selects the main diagonal: 1, 5, 9 → 15.

Q14 — a. v[1] = 5 + 8 = 13 → {5,13,8} printed without separators: 5138. Reads that look ambiguous (5138) are why the labs print separators.

Q15 — a. Empty input has no a[0] to seed from — the guard must precede every element access. Empty-collection handling is a first-class case in every analyzer from here to the capstone.


← CU1… · CU3–CU4 → · Hub