Given `int a = 19, b = 4;`, what will `cout << a / b << " " << a % b;` print?
- A) 4.75 3
- B) 4 3
- C) 3 4
- D) 4 1
Answer & Explanation: B) 4 3 — Because both are `int`, `19 / 4` yields integer quotient `4`, and `19 % 4` yields remainder `3`.
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.
Answer & Explanation: B) 4 3 — Because both are `int`, `19 / 4` yields integer quotient `4`, and `19 % 4` yields remainder `3`.
Answer & Explanation: B) aux = x; x = y; y = aux; — Save `x` in `aux` first, copy `y` into `x`, then restore `aux` into `y`.
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.
Answer & Explanation: B) False — False! `=` is assignment: C++ evaluates the right side first (5 + 3 = 8) and stores 8 back into `x`.
Answer & Explanation: 9 4 — Arithmetic swap without `aux`: a=13, b=13-9=4, a=13-4=9. Output is `9 4`.
| 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 | |