ITPEC FE Subject B October 2025 Question 4
From the answer group below, select the correct combination of answers to be inserted into A through C in the program.
The procedure reverse_digits receives one positive integer argument named num and outputs both the original integer and the integer with its digits reversed. Assume that the ones digit of num is not 0.
Example:Number is 456, output 654Number is 23407, output 70432
Answer group
Explanation
A: temp > 0, B: rev × 10 + rm, C: integer part of (temp ÷ 10) — peel digits off the right of temp and append each one to the right of rev.
- •
nummust be output unchanged, so the loop works on the copytempand stops when its digits are used up: A istemp > 0. - •
rm ← temp mod 10takes the ones digit. Shiftingrevone place left and adding the digit appends it: B isrev × 10 + rm. - •Dropping the ones digit is integer division by 10: C is the integer part of
(temp ÷ 10).
Trace for 456:
temp becomes 0, the loop ends, and "Number is 456, output 654" is printed.
Answer (g)
Why not others:
- •(a), (b), (c) and (d) — A
num > 0tests a variable the loop never changes, so the loop never ends - •(e) — B
rm × 10 + revputs each new digit in the tens place instead of appending it: for 456 it gives 60, 110, 150 and outputs 150 - •(f) and (h) — C
temp - rmonly zeroes the ones digit (456 becomes 450); from then onrmis 0 andtempstays 450, so the loop never ends
Key rule: Take the last digit with mod 10, drop it with integer division by 10, and build the reversed number by multiplying the result so far by 10 before adding the digit; loop on the variable the body changes.
Math & Numbers · AI-written, may contain errors
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.