ITPEC FE Subject B April 2025 Question 4

From the answer group below, select the correct combination of answers to be inserted into A and B in the program.

The following function receives an integer number and returns the result of interpreting the decimal representation of the number as a binary number. Note that the number is non-negative, and its decimal representation comprises only the digits 0 and 1. For instance, if it receives 1100, it returns 12.

[Program]
○ integer: convert(integer: number)
integer: place, n, remainder, decimal
decimal ← 0
place ← 1
n ← number
while (n > 0)
remainder ← n mod 10
n ← integer part of (n ÷ 10)
decimal ← decimal + A
place ← B
endwhile
return decimal

Answer group

OptionAB

Explanation

A: remainder × place, B: 2 × place — each decimal digit is one bit, weighted by a power of two that doubles on every pass.

n mod 10 takes the rightmost digit, which is 0 or 1, and the integer division by 10 drops it. That digit is the current bit, so it contributes bit × weight, with weights 1, 2, 4, 8, … from the right.

Trace with number = 1100:

PassDigitdecimalplace after
100 + 0 × 1 = 02
200 + 0 × 2 = 04
310 + 1 × 4 = 48
414 + 1 × 8 = 1216

n is now 0, the loop ends and the function returns 12, as the example requires.

Answer (g)

Why not others:

  • •(a) and (b) — A = 2 × place adds a weight whether the digit is 0 or 1, so the bit is never used (1100 would give 2 + 4 + 8 + 16 = 30); their B choices are also not a doubling.
  • •(c) and (d) — A = 10 × place likewise ignores the digit and adds decimal-sized amounts.
  • •(e) and (f) — A = n × place multiplies by the rest of the number (110, 11, 1, 0 for 1100) instead of the single digit.
  • •(h) — the right A, but B = 10 × place gives decimal weights 1, 10, 100, 1000, so 1100 comes back as 1100.

Key rule: To reinterpret digits, split them off with mod and div of the source base, and multiply each by a weight that grows by the target base.

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.