IPA IP 2022 Question 78

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

The function checkDigit receives, as the argument, the integer-type array originalDigit in which the digits of each place of a 9-digit decimal integer are stored in order from the highest place, and returns, as the return value, the check digit calculated by the following procedure. Which of the following is the appropriate piece of code to be inserted into A in the program? Here, the array index starts at 1.

[Procedure]

(1)Total the values of the elements with indexes 1 through 9 of the array originalDigit.(2)If the total value is greater than 9, total the digits of each place when the total value is expressed as a decimal integer. Repeat this operation until the total value becomes 9 or less.(3)Use the value obtained in (2) as the check digit.
[Program]
○ integer: checkDigit(integer []: originalDigit)
integer: i, j, k
j ← 0
for (increase i from 1 to the number of elements in originalDigit by 1)
j ← j + originalDigit[i]
endfor
while (j is greater than 9)
k ← the quotient of j ÷ 10 /* For a 9-digit decimal number, j never exceeds 2 digits */
A
endwhile
return j

Explanation

j ← k + (j − 10 × k) — inside the loop k is the tens digit of j, so j − 10 × k is its ones digit, and the blank must add the two.

Since j has at most 2 digits, k = j ÷ 10 (quotient) is the tens digit and j − 10 × k is the remainder, the ones digit. Their sum is the digit sum that step (2) asks for, and the loop repeats it while j is greater than 9.

Trace for 9 digits 999999999: j = 81, so k = 8 and j = 8 + 1 = 9; the loop ends and 9 is returned. For a total of 46: 4 + 6 = 10, then 1 + 0 = 1.

Answer (b)

Why not others:

  • •(a) — keeps only the ones digit and drops the tens: a total of 46 gives 6, not 1
  • •(c) — k + (j − 10) × k is not a digit sum and grows: 46 gives 4 + 36 × 4 = 148, and the loop never ends
  • •(d) — k + j only increases j, so j stays above 9 and the loop never ends

Key rule: For a non-negative integer, the quotient by 10 is everything but the ones digit and j − 10 × (j ÷ 10) is the ones digit (the remainder); summing digits repeatedly gives the digital root.

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.