IPA IP 2026 Question 85
A question IPA Japan published in 令和8年度 ITパスポート試験 公開問題 問85, translated into English. ITPEC is based on the same exams.
The function isPrime returns true as the return value if the positive integer given as its argument is a prime number, and false if it is not a prime number. For example, when the function isPrime is called as isPrime(, the return value is true. Which of the following is the appropriate combination of words to be inserted into A and B in the program?
Explanation
A: less than, B: greater than — reject numbers below 2, then try every divisor from 2 up to num − 1.
The first if must reject only 1: prime numbers start at 2, and isPrime( must return true. So A is “less than”: num < 2.
The loop tries div = 2, 3, … and returns false as soon as one divides num evenly. It must stop before div reaches num itself, since num always divides itself. So B is “greater than”: the loop runs while num > div.
- •
isPrime(: 1 < 2, returns1) false - •
isPrime(: 2 < 2 is false; 2 > 2 is false, so the loop is skipped and2) trueis returned - •
isPrime(: remainder of 9 ÷ 2 is 1, of 9 ÷ 3 is 0, returns9) false - •
isPrime(: 2 to 6 leave remainders,7) divreaches 7, loop ends, returnstrue
Run for 1 to 199, only this combination returns true for exactly the primes.
Answer (d)
Why not others:
- •(a) and (b) — A “less than or equal to” makes
isPrime(return2) false, contradicting the example in the question - •(c) — B “equal to”: for 2 the loop runs with
div= 2 and returnsfalse; for any larger number the loop never runs, so 4 or 9 returnstrue
Key rule: Check a loop's stopping condition at the boundaries: here the smallest input (2) and the case where the divisor reaches the number itself.
Algorithm and Programming · AI-written, may contain errors
This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.