IPA IP 2024 Question 62

A question IPA Japan published in 令和6年度 ITパスポート試験 公開問題 問62, translated into English. ITPEC is based on the same exams.

The function convert is a program that converts an integer-type array into a string according to a certain rule. When the function convert is called as convert(arrayInput), which of the following is the value of the argument arrayInput for which the return value is “AABAB”? Here, the number of elements in arrayInput is 1 or more, and the array index starts at 1.

[Program]
○ string: convert(integer []: arrayInput)
string: stringOutput ← "" // Store an empty string
integer: i
for (increase i from 1 to the number of elements in arrayInput by 1)
if (arrayInput[i] is equal to 1)
add "A" to the end of stringOutput
else
add "B" to the end of stringOutput
endif
endfor
return stringOutput

Explanation

{1, 1, 2, 1, 0} — each element that equals 1 becomes “A” and every other value becomes “B”, so “AABAB” needs 1, 1, not 1, 1, not 1.

The loop visits the elements in order and adds one letter per element to the end of stringOutput. Running the function on each answer choice:

ChoicearrayInputReturn value
(a){0, 0, 1, 2, 1}BBABA
(b){0, 1, 2, 1, 1}BABAA
(c){1, 0, 1, 2, 0}ABABB
(d){1, 1, 2, 1, 0}AABAB

Only (d) has 1 in positions 1, 2 and 4 and a value other than 1 in positions 3 and 5.

Answer (d)

Why not others:

  • •(a) — starts with two 0s, so the result starts with “BB”; it is the exact opposite, BBABA
  • •(b) — position 1 holds 0 and position 5 holds 1, giving BABAA
  • •(c) — positions 2 and 3 are the wrong way round (0, then 1), giving ABABB

Key rule: The else branch catches every value that fails the condition: 0 and 2 both become “B”, so only the positions of the 1s matter.

Algorithm and Programming · 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.