IPA IP 2025 Question 78
A question IPA Japan published in 令和7年度 ITパスポート試験 公開問題 問78, translated into English. ITPEC is based on the same exams.
Which of the following is the output when the procedure calcMod3 is called?
Explanation
9 — the loop adds up the values of i from 1 to 7 that are divisible by 3, which are 3 and 6.
totalValue starts at 0. For each i from 1 to 7, the remainder of i ÷ 3 is checked, and i is added only when the remainder is 0:
After the loop totalValue is 9, and that is output.
Answer (d)
Why not others:
- •(a) 2 — the number of multiples of 3 from 1 to 7: counting the hits instead of adding them up
- •(b) 3 — only the first multiple of 3, as if the loop ended after
i= 3 - •(c) 7 — the last value of
i, which is also the number of times the loop runs
Key rule: Trace a loop as a table with one row per pass, and check whether the program counts the matching values or adds them up.
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.