📝
ArkyEdu TIC
RO Solve Interactive
Grade 7 · Worksheet · 30 min · 100p (10p ex officio)

Grade 7 C++ Worksheet: Integer Division (`/`), Modulo (`%`) & 3-Cup Swap (with RAM Trace)

Quick Summary & Key Points:

Covers C++ linear basics (100 pts): integer division (`17 / 5 == 3`), last digit extraction via `% 10`, digit truncation via `/ 10`, assignment `=` vs equality `==`, and safe 3-variable swapping (`aux = a; a = b; b = aux;`).

Theory Snapshot: For `int`: `n % 10` gives the last digit (`247 % 10 == 7`) and `n / 10` removes it (`247 / 10 == 24`). Never swap via `a = b; b = a;`—you lose the original `a` unless you save it in `aux` first.

1. Part I · 1 (18p)

Given `int a = 19, b = 4;`, what will `cout << a / b << " " << a % b;` print?

Answer & Explanation: B) 4 3 — Because both are `int`, `19 / 4` yields integer quotient `4`, and `19 % 4` yields remainder `3`.

2. Part I · 2 (18p)

Which code snippet CORRECTLY swaps the values of `x` and `y`?

Answer & Explanation: B) aux = x; x = y; y = aux; — Save `x` in `aux` first, copy `y` into `x`, then restore `aux` into `y`.

3. Part II · 1 (18p)

Enter the exact output printed by the C++ snippet below:

int n = 385;
int s = (n / 100) + (n / 10 % 10) + (n % 10);
cout << s;

Answer & Explanation: 16 — 385 / 100 = 3, 385 / 10 % 10 = 8, 385 % 10 = 5. Total sum = 3 + 8 + 5 = 16.

4. Part II · 2 (18p)

True or False: Writing `int x = 5;` followed by `x = x + 3;` is an invalid statement in C++.

Answer & Explanation: B) False — False! `=` is assignment: C++ evaluates the right side first (5 + 3 = 8) and stores 8 back into `x`.

5. Part III (18p)

What does `cout << a << " " << b;` output after: `int a = 4, b = 9; a = a + b; b = a - b; a = a - b;`? (two integers separated by space)

Answer & Explanation: 9 4 — Arithmetic swap without `aux`: a=13, b=13-9=4, a=13-4=9. Output is `9 4`.

Official 100-Point Grading Rubric (Grade 1–10)

Section Evaluated Competency Points
Part I (2 × 18 pts) Integer division `/`, modulo `%`, and `aux` swap 36p
Part II (2 × 18 pts) Digit extraction & assignment semantics 36p
Part III (18 pts) Tracing arithmetic swap sequence 18p
Ex officio Baseline points 10p
TOTAL (Grade = Score / 10) 100p

More Worksheets & Related Links