ITPEC IP April 2026 Question 6

Source exam: ITPEC IP April 2026Topic: Algorithm and Programming

ITPEC IP April 2026 — Question 6 of 100

Save A in TMP, copy B to A, then copy TMP to B — the temporary variable preserves the value that the first assignment would otherwise destroy.

Let the original values be A = x and B = y. The sequence in option (b) gives:

  • TMP ← A, so TMP holds x
  • A ← B, so A becomes y
  • B ← TMP, so B becomes x

The final values are therefore exchanged.

Answer (b)

Why not others:
- (a) finishes with B ← A after A already became y, leaving both variables equal to y

- (c) saves B, then overwrites A with B and restores B from TMP; both remain y

- (d) overwrites A and then copies that overwritten value to B, again leaving both equal to y

Key rule: Before overwriting one swap operand, save its original value; the final assignment must restore that saved value into the other operand.

AI-generated — may contain errors

The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.

This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official IP past-paper collection or Report an issue.