Skip to content

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

This question's explanation and your answers so far.

問85
頻出度★★☆

関数binaryToIntegerは,1桁以上の符号なし2進数を文字列で表した値を引数binaryStrで受け取り,その値を整数に変換した結果を戻り値とする。例えば,引数として“100”を受け取ると,4を返す。プログラム中のa,bに入れる字句の適切な組合せはどれか。 〔プログラム〕 ○整数型: binaryToInteger(文字列型: binaryStr)  整数型: integerNum, digitNum, exponent, i  integerNum ← 0  for (i を 1 から binaryStr の文字数 まで 1 ずつ増やす)   digitNum ← binaryStr の末尾から i 番目の文字を整数型に変換した値   // 例: 文字“1”であれば整数値1に変換   exponent ← [ a ]   integerNum ← [ b ]  endfor  return integerNum

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

問 85

  • Incorrectア

    a's “(2 to the power of i) − 1” is wrong. The weight of the i-th digit from the end is 2 to the power of (i − 1), not (2 to the power of i) − 1 (e.g., at i=1 the weight is 1, and this expression happens to give 2−1=1 which matches, but at i=2 it gives 3 instead of the correct 2). Furthermore, b's “integerNum × digitNum × exponent” is also wrong: multiplying the digits together always yields 0 because the initial value is 0, so the conversion cannot be done. Both a and b are inappropriate.

  • Incorrectイ

    b's “integerNum + digitNum × exponent” is correct cumulative addition, but a's “(2 to the power of i) − 1” is wrong. The weight of the i-th digit from the end is 2 to the power of (i − 1) (1 at i=1, 2 at i=2, 4 at i=3), whereas (2 to the power of i) − 1 (3 at i=2, 7 at i=3) does not match the digit weights, so even substituting “100” does not yield 4.

  • Incorrectウ

    a's “2 to the power of (i − 1)” is the correct digit weight, but b's “integerNum × digitNum × exponent” is wrong. Since integerNum starts at 0, updating it by multiplication keeps multiplying by 0 every time, making the return value always 0. The correct operation must be addition that adds in each digit's contribution.

  • エ

    正解 In binary, the weight of the rightmost digit (the 1st from the end) is 2 to the power of 0, the one to its left is 2 to the power of 1, and so on, increasing; therefore the weight of the i-th digit from the end is “2 to the power of (i − 1)”. This goes into a (exponent ← 2 to the power of (i − 1)). For each digit, the digit value digitNum multiplied by the weight exponent is added into the running total, so b becomes “integerNum + digitNum × exponent”, and the whole value is obtained by cumulative addition. Actually substituting “100”, at i=1 the weight gives 1×0=0, at i=2 the weight gives 2×0=0, at i=3 the weight gives 4×1=4, so integerNum is 4. This matches the question's return value 4, so combination エ is correct.

ここがポイント
  • The weight of each digit of an unsigned binary number is 2 to the power of (i − 1) for the i-th digit from the end (least significant). Grasp the place-value scheme where the least significant digit starts at 2 to the power of 0 = 1.
  • Compute the total with a cumulative-addition pattern that “adds in” each digit's value × weight into the running total (integerNum ← integerNum + term). Note that multiplying the initial value 0 keeps it at 0.
用語

Your answers