Skip to content

令和4年度 ITパスポート試験 問96

This question's explanation and your answers so far.

問96
頻出度★★☆

関数calcXと関数calcYは,引数inDataを用いて計算を行い,その結果を戻り値とする。関数calcXをcalcX(1)として呼び出すと,関数calcXの変数numの値が,1→3→7→13と変化し,戻り値は13となった。関数calcYをcalcY(1)として呼び出すと,関数calcYの変数numの値が,1→5→13→25と変化し,戻り値は25となった。プログラム中のa,bに入れる字句の適切な組合せはどれか。 〔プログラム 1〕 ○整数型: calcX(整数型: inData)  整数型: num, i  num ← inData  for (i を 1 から 3 まで 1 ずつ増やす)   num ←  a  endfor  return num 〔プログラム 2〕 ○整数型: calcY(整数型: inData)  整数型: num, i  num ← inData  for ( b )   num ←  a  endfor  return num

出典 令和4年度 ITパスポート試験 問96
解説

問 96

  • Incorrectア

    Here a is 2×num+i. Substituting it into calcX, i=1 gives 2×1+1=3 which matches, but i=2 gives 2×3+2=8, not the required 7. The expression for a is wrong and cannot reproduce the change sequence 1→3→7→13.

  • Incorrectイ

    Here a is 2×num+i, which like ア cannot reproduce calcX (i=2 yields 8 instead of 7). Although the range in b is correct, the combination is unsuitable because a is wrong.

  • Incorrectウ

    a = num+2×i is correct, but with b as “i from 1 to 7, increasing by 3 each time”, i = 1, 4, 7 — three times — and the first step i=1 gives 1+2×1=3, not the required 5, so it cannot reproduce calcY's 1→5→13→25. b is wrong.

  • エ

    正解 First determine a using calcX. calcX(1) starts num at 1, and the for loop moves the loop variable i as 1→2→3, three times in all. Substituting a = num+2×i gives 1+2×1=3 at i=1, 3+2×2=7 at i=2, and 7+2×3=13 at i=3, matching the change sequence 1→3→7→13 and the return value 13. Hence a = num+2×i. Next determine b using calcY. calcY uses the same a = num+2×i and changes num as 1→5→13→25 over three steps, so its loop must also run three times. If b is “i from 2 to 6, increasing by 2 each time”, then i = 2, 4, 6 — three times — giving 1+2×2=5 at i=2, 5+2×4=13 at i=4, and 13+2×6=25 at i=6, which matches. Therefore the combination エ, with a = num+2×i and b = “i from 2 to 6, increasing by 2 each time”, is correct.

ここがポイント
  • Tracing a for loop: starting from the initial num, repeatedly apply the loop body's assignment expression for each value of i, and work backward to choose the expression (a) and the loop range (b) that match the given change sequence and return value.
  • How to read the pseudocode for “i from ○ to △, increasing by □ each time”: correctly count the sequence of i and the number of iterations (e.g., from 2 to 6 by 2 → i = 2, 4, 6, three times).
用語

Your answers