Skip to content

令和6年度 ITパスポート試験 問62

This question's explanation and your answers so far.

問62
頻出度★★☆

関数 convert は, 整数型の配列を一定のルールで文字列に変換するプログラムである。関数 convert を convert(arrayInput)として呼び出したときの戻り値が“AABAB”になる引数 arrayInput の値はどれか。ここで, arrayInput の要素数は 1 以上とし, 配列の要素番号は 1 から始まる。 〔プログラム〕 〇文字列型: convert(整数型の配列: arrayInput) 文字列型: stringOutput ← “” // 空文字列を格納 整数型: i for (i を 1 から arrayInput の要素数 まで 1 ずつ増やす) if (arrayInput[i] が 1 と等しい) stringOutput の末尾 に “A” を追加する else stringOutput の末尾 に “B” を追加する endif endfor return stringOutput

出典 令和6年度 ITパスポート試験 問62
解説

問 62

  • Incorrectア

    {0, 0, 1, 2, 1} gives 0→B, 0→B, 1→A, 2→B, 1→A, producing “BBABA”. The first two characters are B, so it does not match “AABAB”.

  • Incorrectイ

    {0, 1, 2, 1, 1} gives 0→B, 1→A, 2→B, 1→A, 1→A, producing “BABAA”. The first character is B and the last is A, so it does not match “AABAB”.

  • Incorrectウ

    {1, 0, 1, 2, 0} gives 1→A, 0→B, 1→A, 2→B, 0→B, producing “ABABB”. The pattern from the second character onward does not match “AABAB”.

  • エ

    正解 The program scans the array arrayInput in order from element index 1 to the end, and if an element value equals 1 it appends “A” to the end, otherwise it appends “B”, building up the string. For the return value to become “AABAB”, the characters from the first must be A, A, B, A, B in order, so each element must be “1, 1, not 1, 1, not 1”. Substituting エ {1, 1, 2, 1, 0} gives 1→A, 1→A, 2→B, 1→A, 0→B, producing “AABAB”, so it is correct.

ここがポイント
  • Tracing pseudocode: use the for loop to scan the array in order from element index 1, and follow the flow element by element, where the if (selection) condition “the element equals 1” branches the output into “A” or “B”.
  • The approach of working backward from the target output string: map each character A/B in “AABAB” to A = element is 1, B = element is not 1, identify the condition each element must satisfy, and check it against the choices.
用語

Your answers