ITPEC FE Subject B April 2025 Question 3

From the answer group below, select the correct answer to be inserted into blank in the description.

The greatest common divisor (GCD) of two numbers is the largest number that divides both of them. The function GCD receives two positive integer numbers and returns their GCD. When the function GCD is called as GCD(98, 56), the output is blank in (1)-(4) below. Here, the output statement “output m, n” outputs the values of variables m and n, and subsequently starts a new line.

(1)98 5642 1428 1414 7
(2)42 5642 1428 1414 14
(3)42 5642 2814 2814 14
(4)56 4242 1414 2814 14
[Program]
○ integer: GCD(integer: x, integer: y)
integer: m ← x
integer: n ← y
while (m ≠ n)
if (m > n)
m ← m - n
else
n ← n - m
endif
output m, n
endwhile
return m

Answer group

Explanation

(2): 42 56, 42 14, 28 14, 14 14 — the output statement comes after the subtraction, so even the first line already shows a changed value.

GCD is Euclid's algorithm by subtraction: the larger value is reduced by the smaller until they are equal.

PassTestUpdateOutput
198 > 56m ← 98 − 56 = 4242 56
242 < 56n ← 56 − 42 = 1442 14
342 > 14m ← 42 − 14 = 2828 14
428 > 14m ← 28 − 14 = 1414 14

Now m = n = 14, the loop ends and the function returns 14, the GCD of 98 and 56. Four lines were printed, matching (2).

Answer (b)

Why not others:

  • •(a) — (1) prints the starting values 98 56 first, as if output came before the update, and its last line 14 7 cannot occur: from 28 14 the program sets m ← 14, it never halves n.
  • •(c) — (3) has 42 28 on the second line, but pass 2 subtracts m from n, 56 − 42 = 14, not 28.
  • •(d) — (4) opens with 56 42, printing n before m, and its third line 14 28 contradicts pass 3, which reduces m to 28 while n stays 14.

Key rule: Before matching a trace against the choices, locate the output statement relative to the updates in the loop body.

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.