IPA FE Subject B sample Question 1

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

From the answer group below, select the correct answer to be inserted into blank in the description.

When the program is executed, the output is “blank”.

[Program]
integer: x ← 1
integer: y ← 2
integer: z ← 3
x ← y
y ← z
z ← x
output the value of y and the value of z in this order, separated by a comma

Answer group

Explanation

3,2 — trace the assignments in order; each one copies the value its right-hand side holds at that moment.

Starting state: x = 1, y = 2, z = 3.

  • •x ← y — x becomes 2, and the original 1 is gone
  • •y ← z — y becomes 3
  • •z ← x — x now holds 2, so z becomes 2

The values of y and z are output in this order, separated by a comma: 3,2.

Answer (f)

Why not others:

  • •(a) 1,2, (b) 1,3, (c) 2,1 and (d) 2,3 — start with a value other than 3, but y copies z before z is changed, so y is 3; (d) is just the initial y and z, as if no assignment ran
  • •(e) 3,1 — gives z the original x of 1, missing that x ← y already overwrote it with 2

Key rule: Keep a table of the variables and update it line by line; an assignment copies the current value and the target's old value is lost, which is why swapping two variables needs a temporary one.

Array Manipulation · 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.