IPA FE Subject B 2025 Question 1
A question IPA Japan published in 令和7年度 基本情報技術者試験 科目B 公開問題 問1, translated into English. ITPEC is based on the same exams.
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
When the arguments received by the function function1 and the arguments received by the function function2 are the same, the two functions return the same value. Here, the arguments n and m are positive integers, and the argument m is a number that is greater than the argument n by 10 or more.
Answer group
Explanation
A: increase i from 1 to 3 by 1, B: increase j from tempN by 4, not exceeding m — function1 tests every number from n to m and counts the multiples of 4; function2 must count the same multiples by finding the first one and then jumping from multiple to multiple.
The first loop of function2 moves tempN up from n until it is a multiple of 4. How many increments that takes depends on n mod 4:
- •remainder 0: the first test exits at once,
tempN=n - •remainder 3: one increment, the second test exits
- •remainder 2: two increments, the third test exits
- •remainder 1:
n,n + 1andn + 2all fail, so the body must run three times; after the third incrementtempN=n + 3, a multiple of 4, and the loop ends by its count
So A must allow three passes: i from 1 to 3. tempN is now the smallest multiple of 4 that is at least n, and the multiples of 4 up to m are tempN, tempN + 4, tempN + 8, …, so B steps j from tempN by 4 while it does not exceed m, counting one per step. Because m is at least n + 10, tempN (at most n + 3) never passes m.
Check with n = 1, m = 11: function1 counts 4 and 8, giving 2. In function2, tempN goes 1 → 2 → 3 → 4 in three passes, then j takes 4 and 8, giving 2.
Answer (f)
Why not others:
- •(a), (b) and (c) — A runs the first loop only twice, so when n mod 4 = 1
tempNstops atn + 2, not a multiple of 4. Forn= 1,m= 11,tempNis 3: (c) counts 3, 7, 11 and returns 3 instead of 2; (a) counts 1, 4, 7, 10 (4) and (b) every number from 3 to 11 (9) - •(d) — B starts at
nand steps bytempN, which counts nothing meaningful: forn= 1,m= 11 it takes 1, 5, 9 and returns 3 - •(e) — B steps by 1, so it counts every number from
tempNtom, not only the multiples of 4: 4 to 11 gives 8
Key rule: To count the multiples of k in a range without testing every number, move up to the first multiple (at most k − 1 increments, so the aligning loop needs k − 1 passes) and then step by k.
Math & Numbers · 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.