IPA IP 2022 Question 96

A question IPA Japan published in 令和4年度 ITパスポート試験 公開問題 問96, translated into English. ITPEC is based on the same exams.

The functions calcX and calcY perform calculations by using the argument inData, and make the results their return values. When the function calcX was called as calcX(1), the value of the variable num of the function calcX changed as 1→3→7→13, and the return value was 13. When the function calcY was called as calcY(1), the value of the variable num of the function calcY changed as 1→5→13→25, and the return value was 25. Which of the following is the appropriate combination of words to be inserted into A and B in the programs?

[Program 1]
○ integer: calcX(integer: inData)
integer: num, i
num ← inData
for (increase i from 1 to 3 by 1)
num ← A
endfor
return num
[Program 2]
○ integer: calcY(integer: inData)
integer: num, i
num ← inData
for (B)
num ← A
endfor
return num
OptionAB

Explanation

A: num + 2 × i, B: increase i from 2 to 6 by 2 — fix A with calcX, whose loop is given, then pick the loop of calcY that gives 1→5→13→25.

calcX(1) runs with i = 1, 2, 3 from num = 1:

Expression for Ai = 1i = 2i = 3
2 × num + i3819
num + 2 × i3713

Only num + 2 × i gives 1→3→7→13, so A is num + 2 × i. In calcY each step adds 2 × i, and the steps are +4, +8, +12, so i takes 2, 4, 6: increase i from 2 to 6 by 2. Check: 1 + 4 = 5, 5 + 8 = 13, 13 + 12 = 25.

Answer (d)

Why not others:

  • •(a) and (b) — with 2 × num + i the function calcX gives 1→3→8→19, not 13
  • •(c) — i = 1, 4, 7 adds 2, 8, 14, giving 1→3→11→25: the return value is 25, but num does not pass through 5 and 13

Key rule: When two programs share a blank, settle it in the program with fewer unknowns first, then trace every intermediate value, not just the return value.

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.