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.
Answer group
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:
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 × placeadds 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 × placelikewise ignores the digit and adds decimal-sized amounts. - •(e) and (f) — A =
n × placemultiplies by the rest of the number (110, 11, 1, 0 for 1100) instead of the single digit. - •(h) — the right A, but B =
10 × placegives 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.