IPA FE Subject B 2026 Question 3

A question IPA Japan published in 令和8年度 基本情報技術者試験 科目B 公開問題 問3, translated into English. ITPEC is based on the same exams.

From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array index starts at 1.

When the argument given to the function func1 and the argument given to the function func2 are the same, the two functions return the same value. The program must not refer to any area outside the bounds of the array.

[Program]
○ integer: func1(integer: n)
if (n is 2 or less)
return 1
endif
return 2 × func1(n - 2) + func1(n - 1)
○ integer: func2(integer: n)
integer []: data ← {1, 1, 1}
integer: i
/* The loop is not executed when n is less than 3 */
for (increase i from 3 to n by 1)
data[1] ← data[2]
data[2] ← data[3]
data[3] ← blank
endfor
return data[3]

Answer group

Explanation

2 × data[1] + data[2] — func2 computes the same recurrence as func1 with a three-slot window: shift the window left, then compute the new value from the two older slots.

func1 defines f(1) = f(2) = 1 and f(n) = 2 × f(n − 2) + f(n − 1). In func2, at the start of the pass for i, data holds f(i − 3), f(i − 2), f(i − 1) (all 1 before the first pass). The two assignments shift them left, so data[1] is f(i − 2) and data[2] is f(i − 1), and f(i) is 2 × data[1] + data[2].

idata after the passf(i)
3{1, 1, 3}3
4{1, 3, 5}5
5{3, 5, 11}11
6{5, 11, 21}21

func1(6) is also 21, and for n below 3 both return 1.

Answer (a)

Why not others:

  • •(b) — doubles the newer value instead of the older: f(4) would be 2 × 3 + 1 = 7, not 5
  • •(c) and (d) — index data by the loop counter; at i = 4 they give 9, and from i = 5 data[i - 1] is data[4], outside the three-element array
  • •(e) and (f) — also add the old data[3], which after the shift is a copy of f(i − 1), so f(3) comes out 4
  • •(g) and (h) — add the old data[3] and index by i as well: 4 at i = 3, then out of bounds from i = 5

Key rule: To turn a recurrence into a loop, keep a sliding window of the last few values: shift the window, then compute the new value from the shifted slots, not from the loop counter.

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.