ITPEC FE Subject B October 2025 Question 5

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

Prime numbers are the positive integers that are only divisible by the number itself and 1. Procedure printPrime receives the integer N (N ≥ 1) as an argument and prints the first N prime numbers. For example, printPrime(6) will print “2 3 5 7 11 13”.

[Program]
○ printPrime(integer: N)
integer: count, number, i
boolean: isPrime
count ← 1
number ← 2
while (count ≤ N)
isPrime ← true
for (increase i from 2 to integer part of the square root of number by 1)
if (number mod i = 0)
A
exit the for block
endif
endfor
if (isPrime = true)
output " ", number
B
endif
number ← number + 1
endwhile

Answer group

OptionAB

Explanation

A: isPrime ← false, B: count ← count + 1 — a divisor clears the prime flag, and each prime printed raises the count of primes found.

  • •isPrime is set to true before each number is tested. A divisor between 2 and the square root of number proves it is not prime, so A clears the flag before the for loop is left.
  • •count starts at 1 and the loop runs while count ≤ N, so it holds the number of primes printed so far plus one. Each prime printed must raise it by exactly 1: B is count ← count + 1.

Trace for printPrime(6): 2 and 3 have no candidate divisors and are printed; 4, 6, 8, 9, 10 and 12 each hit a divisor; 5, 7, 11 and 13 pass. After 13 count becomes 7, the loop ends, and 2 3 5 7 11 13 has been printed.

Answer (c)

Why not others:

  • •(a) — never clears isPrime, so every number is printed, and count rises on composites instead of primes: printPrime(6) prints 2 to 12
  • •(b) — the same missing flag, with count raised by the divisor i on each composite
  • •(d) — A is right, but B adds the loop variable i, whose value after the loop has nothing to do with the number of primes found (for 2 and 3 the loop does not run at all)

Key rule: In a search-and-count loop, set the failure flag at the point of failure, and advance the counter only where success is confirmed.

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.