ITPEC IP October 2023 Question 6

Source exam: ITPEC IP October 2023Topic: Algorithm and Programming

ITPEC IP October 2023 — Question 6 of 100

To exchange A and B, first preserve A in TMP, then copy B into A, and finally copy the preserved value into B. The correct sequence is TMP ← A, A ← B, B ← TMP. If the original values are A = α and B = β, the final values become A = β and B = α.

Answer (b)

Why not others:
- (a) finishes with B ← A after A was overwritten, so both variables become the original B

- (c) saves B but finishes by restoring B from TMP, leaving both variables as the original B

- (d) saves B and then copies overwritten A back to B, again failing to preserve the original A

Key rule: Before overwriting the first variable, save its original value; the final assignment must restore that saved value into the second variable.

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.