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
Answer group
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 incolors, 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.