ITPEC FE Subject B April 2024 Question 4
ITPEC FE Subject B April 2024 — Question 4 of 20
Identifying digit-extraction operations: num mod 10 gives the last digit, integer part of (num ÷ 10) removes it.
The function sumDigits adds up all digits of a non-negative integer. Each loop iteration must:
- Extract the last digit using
num mod 10and add it tosum - Remove the last digit using
integer part of (num ÷ 10)
Trace with num = 123:
| Iteration | num | num mod 10 | sum | num ← integer part of (num ÷ 10) |
|---|---|---|---|---|
| 1 | 123 | 3 | 0 + 3 = 3 | 12 |
| 2 | 12 | 2 | 3 + 2 = 5 | 1 |
| 3 | 1 | 1 | 5 + 1 = 6 | 0 |
Loop ends (num = 0), returns 6. Correct: 1 + 2 + 3 = 6.
So A = sum + num mod 10, B = integer part of (num ÷ 10) → (b).
Why not others:
- (a) — B is num mod 10, which takes the last digit instead of removing it — num never shrinks, infinite loop
- (c, d) — sum × 10 builds a number from digits (like reversing), not summing them
- (e, f) — A uses integer part of (num ÷ 10) which grabs all digits except the last — wrong extraction
- (g, h) — combine both errors: sum × 10 (number building) and integer part of (num ÷ 10) in A
Key rule: mod 10 extracts the last digit; integer part of (÷ 10) removes it — these two always work as a pair for digit-by-digit processing.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.