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 654

Number is 23407, output 70432

[Program]
○ reverse_digits(integer: num)
integer: rev ← 0
integer: rm ← 0
integer: temp ← num
while (A)
rm ← temp mod 10
rev ← B
temp ← C
endwhile
output "Number is ", num, ", output ", rev

Answer group

OptionABC

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.

  • •num must be output unchanged, so the loop works on the copy temp and stops when its digits are used up: A is temp > 0.
  • •rm ← temp mod 10 takes the ones digit. Shifting rev one place left and adding the digit appends it: B is rev × 10 + rm.
  • •Dropping the ones digit is integer division by 10: C is the integer part of (temp ÷ 10).

Trace for 456:

temp at startrmrev
45666
45565
44654

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 > 0 tests a variable the loop never changes, so the loop never ends
  • •(e) — B rm × 10 + rev puts 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 - rm only zeroes the ones digit (456 becomes 450); from then on rm is 0 and temp stays 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.