IPA FE Subject A sample Question 2

A question IPA Japan published in 基本情報技術者試験 科目A サンプル問題 問2, translated into English. ITPEC is based on the same exams.

The flowchart below shows the process of converting a decimal integer j (0 < j < 100) into an 8-digit binary number. The binary number is stored in array elements NISHIN(1) through NISHIN(8), starting from the lowest-order digit. Which of the following is the appropriate combination of processes to be inserted into blanks A and B in the flowchart? Here, j div 2 represents the integer part of the quotient when j is divided by 2, and j mod 2 represents the remainder when j is divided by 2.

StartInput jConversionk: 1, 1, 8 (Note)ABConversionEnd

(Note) The repetition specification of a loop limit shows variable name: initial value, increment, final value.

OptionAB

Explanation

A: NISHIN(k) ← j mod 2, B: j ← j div 2 — store the remainder as the next digit, then halve j for the next pass.

Repeated division by 2 produces the binary digits from the lowest one up: the remainder of each division is the next digit, and the quotient is what gets divided next.

In pass k, A stores j mod 2 in NISHIN(k), and B replaces j with j div 2. The order matters: the remainder must be read before j is overwritten.

Trace j = 6: pass 1 stores 0 and j becomes 3; pass 2 stores 1 and j becomes 1; pass 3 stores 1 and j becomes 0; passes 4 to 8 store 0. Read from NISHIN(8) down to NISHIN(1), the digits are 00000110, which is 6.

Answer (d)

Why not others:

  • •(a) — halves j before taking the remainder, so the lowest digit is lost: for 6 it stores 1, 1, 0, … and gives 00000011, which is 3
  • •(b) — replaces j with its remainder (0 or 1) and stores the quotient of that, which is always 0: every digit comes out 0
  • •(c) — stores the quotient instead of the remainder, which is not a binary digit at all: for 6 the first element gets 3

Key rule: To convert to binary by repeated division, the remainder is the next digit from the bottom, and it must be taken before the number is replaced by the quotient.

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.