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( will print “2 3 5 7 11 13”.
Answer group
Explanation
A: isPrime ← false, B: count ← count + 1 — a divisor clears the prime flag, and each prime printed raises the count of primes found.
- •
isPrimeis set to true before eachnumberis tested. A divisor between 2 and the square root ofnumberproves it is not prime, so A clears the flag before the for loop is left. - •
countstarts at 1 and the loop runs whilecount ≤ N, so it holds the number of primes printed so far plus one. Each prime printed must raise it by exactly 1: B iscount ← count + 1.
Trace for printPrime(: 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, andcountrises on composites instead of primes:printPrime(prints 2 to 126) - •(b) — the same missing flag, with
countraised by the divisorion 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.