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(2), 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?

[Program]
○ boolean: isPrime(integer: num)
integer: div ← 2
if (num is A 2)
return false
else
while (num is B div)
if (the remainder of num ÷ div is equal to 0)
return false
else
div ← div + 1
endif
endwhile
return true
endif
OptionAB

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(2) 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): 1 < 2, returns false
  • •isPrime(2): 2 < 2 is false; 2 > 2 is false, so the loop is skipped and true is returned
  • •isPrime(9): remainder of 9 ÷ 2 is 1, of 9 ÷ 3 is 0, returns false
  • •isPrime(7): 2 to 6 leave remainders, div reaches 7, loop ends, returns true

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(2) return false, contradicting the example in the question
  • •(c) — B “equal to”: for 2 the loop runs with div = 2 and returns false; for any larger number the loop never runs, so 4 or 9 returns true

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.