IPA FE Subject B sample Question 3
A question IPA Japan published in 基本情報技術者試験 科目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 description. Here, the array index starts at 1.
The function makeNewArray receives an integer-type array with two or more elements as its argument and returns an integer-type array. When the function makeNewArray is called as makeNewArray(, the value of the element at index 5 of the returned array is blank.
Answer group
Explanation
17 — each pass appends the last element of out plus the next input, so out holds the running totals of in.
out starts as {3}. Each pass reads tail, the last element of out, and appends tail + in[i]:
The element at index 5 is 17, the sum 3 + 2 + 1 + 6 + 5 of the first five inputs.
Answer (f)
Why not others:
- •(a) 5 — the input
in[5], not an element of the returned array - •(b) 6 and (e) 12 —
out[3]andout[4], the running total two passes and one pass too early - •(c) 9 and (d) 11 — add two neighbouring inputs, 5 + 4 and 6 + 5, instead of adding the input to the running total in
tail - •(g) 21 —
out[6], the last element and the sum of all six inputs; index 5 is the one before it
Key rule: A loop that appends “last element + next input” builds prefix sums: element k of the result is the sum of the first k inputs.
Array Manipulation · 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.