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({3, 2, 1, 6, 5, 4}), the value of the element at index 5 of the returned array is blank.

[Program]
○ integer []: makeNewArray(integer []: in)
integer []: out ← {} // array with 0 elements
integer: i, tail
add the value of in[1] to the end of out
for (increase i from 2 to the number of elements in in by 1)
tail ← out[the number of elements in out]
add the result of (tail + in[i]) to the end of out
endfor
return out

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]:

iin[i]tailappendedout
2235{3, 5}
3156{3, 5, 6}
46612{3, 5, 6, 12}
551217{3, 5, 6, 12, 17}
641721{3, 5, 6, 12, 17, 21}

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] and out[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.