IPA FE Subject B 2026 Question 5

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

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

In this question, a representation by an integer-type array in which only one element is 1 and the other elements are 0 is called a One-Hot representation. As shown in the figure, the function oneHotEncoding receives as its argument a string-type array of one or more elements in which names of colors are stored, converts each element into a One-Hot representation based on the names of colors contained in the array (three kinds in the example in the figure), and stores them in an array of integer-type arrays and returns it. An example of conversion by the function oneHotEncoding is shown in the figure.

Example of argument: {"Red", "Green", "Blue", "Red"}

↓

One-Hot representation of "Red": {1, 0, 0}

One-Hot representation of "Green": {0, 1, 0}

One-Hot representation of "Blue": {0, 0, 1}

↓

Return value corresponding to the example of argument: {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 0, 0}}

Figure Example of conversion by the function oneHotEncoding

[Program]
○ integer [][]: oneHotEncoding(string []: colors)
integer: i, j, k
string []: colorVector ← {} // array with 0 elements
integer []: tempVector
integer [][]: oneHotVector ← {} // array with 0 elements
/* Create the list of names */
for (increase i from 1 to the number of elements in colors by 1)
if (the value of colors[i] is not stored in any element of colorVector)
add A to the end of colorVector
endif
endfor
/* Convert to One-Hot representation */
for (increase j from 1 to the number of elements in colors by 1)
tempVector ← {} // array with 0 elements
for (increase k from 1 to the number of elements in colorVector by 1)
if (B)
add 1 to the end of tempVector
else
add 0 to the end of tempVector
endif
endfor
add tempVector to the end of oneHotVector
endfor
return oneHotVector

Answer group

OptionAB

Explanation

A: the value of colors[i], B: colors[j] is equal to colorVector[k] — the first loop collects the distinct colour names, the second puts a 1 only at the position of each element's own name.

The first loop adds colors[i] to colorVector whenever that name is not there yet, so for the example colorVector becomes {"Red", "Green", "Blue"}, one entry per distinct name in order of first appearance. A is therefore the value of colors[i].

The second loop builds one vector per element colors[j], with one entry per name colorVector[k]. The entry must be 1 only where the name is the element's own colour, so B is colors[j] is equal to colorVector[k]. For "Green" (j = 2): Red gives 0, Green gives 1, Blue gives 0, so {0, 1, 0}. The four elements give {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 0, 0}}, as in the figure.

Answer (b)

Why not others:

  • •(a) — B asks whether colorVector[k] appears anywhere in colors, which is true for every collected name, so every vector is {1, 1, 1}
  • •(c) and (d) — add the undefined value: no name is ever found in colorVector, so all four elements add an undefined entry, and since an undefined entry matches no colour every vector is {0, 0, 0, 0}

Key rule: One-hot encoding takes two passes: collect the distinct categories, then for each item put a 1 only at the position of its own category.

Math & Data Science · 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.