Grade 7 · Olympiad / Prep · 40 min · 100p (10p ex officio)
Grade 7 ICT Olympiad & Exam Sheet: `while` Loops, Reversed Digits, Palindromes & Primes
Quick Summary & Key Points:
Competition-ready Grade 7 worksheet (100 pts) covering `while (n > 0)` digit decomposition, reversed number (`rev = rev * 10 + n % 10`), saving `copy = n` for palindrome checks, and prime divisor counting.
Theory Snapshot: Palindrome rule: save `copy = n` BEFORE the `while (n > 0)` loop! After the loop ends, `n` is `0`, so compare `copy == rev` (never `n == rev`).
1. Part I · 1
(18p)
Why does `if (n == og)` placed AFTER `while (n > 0) { og = og * 10 + n % 10; n /= 10; }` fail for almost every input?
- A) Because when `while` ends, `n` has become `0`, so it compares `0 == og` instead of the original value
- B) Because `n % 10` always returns 1
- C) Because `while` rejects `/=`
- D) Because `og` cannot exceed 100
Answer & Explanation: A) Because when `while` ends, `n` has become `0`, so it compares `0 == og` instead of the original value — After `while (n > 0)` terminates, `n == 0`. Always save `int copy = n;` before the loop and compare `copy == og`.
2. Part I · 2
(18p)
What does the following algorithm output when `n = 4072`?
int n = 4072, cnt = 0;
while (n > 0) {
if ((n % 10) % 2 == 0)
cnt++;
n /= 10;
}
cout << cnt;
Answer & Explanation: 3 — Digits of 4072: 2 (even), 7 (odd), 0 (even: 0 % 2 == 0!), and 4 (even). Total = 3 even digits!
3. Part II · 1
(18p)
What is the greatest common divisor `gcd(48, 18)` computed by Euclid’s algorithm?
Answer & Explanation: 6 — 48 % 18 = 12 → 18 % 12 = 6 → 12 % 6 = 0 ⇒ GCD = 6.
4. Part II · 2
(18p)
True or False: The integer 1 is considered a prime number in math and computer science.
Answer & Explanation: B) False — False! A prime number has EXACTLY 2 distinct positive divisors (1 and itself). 1 has only one divisor; the smallest prime is 2.
5. Part III
(18p)
How many digits does `while (n > 0) { cnt++; n /= 10; }` count if the input is `n = 0`?
- A) 1 digit
- B) 0 digits (because 0 > 0 is False immediately; do...while should be used)
- C) Compile error
- D) Infinite loop
Answer & Explanation: B) 0 digits (because 0 > 0 is False immediately; do...while should be used) — For `n = 0`, `while (n > 0)` fails before the first step so `cnt` stays `0`. Switching to `do...while` fixes the `n = 0` case to 1 digit.
Official 100-Point Grading Rubric (Grade 1–10)
| Section |
Evaluated Competency |
Points |
| Part I (2 × 18 pts) |
Palindrome copy bug & counting even digits (incl. 0) |
36p |
| Part II (2 × 18 pts) |
Euclid GCD & primality edge case n=1 |
36p |
| Part III (18 pts) |
Edge case n = 0: while vs do...while |
18p |
| Ex officio |
Baseline points |
10p |
| TOTAL (Grade = Score / 10) |
100p |
More Worksheets & Related Links