ITPEC IP April 2024 Question 6

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

ITPEC IP April 2024 — Question 6 of 100

The final value of x is 5. Start with x = 2 and y = 3, then trace the loop in order.

  • First pass: step (2) changes y from 3 to 2; step (3) sets x = 2 + 2 = 4. Since y is not 1, the procedure repeats.
  • Second pass: step (2) changes y from 2 to 1; step (3) sets x = 4 + 1 = 5. Now y is 1, so the procedure ends.

Thus the accumulated additions are 2 + 2 + 1 = 5.

Answer (b)

Why not others:
- (a) 4 is the intermediate value after only the first pass

- (c) 7 and (d) 8 result from adding values not prescribed by the loop or continuing after the termination condition is met

Key rule: Trace assignments sequentially using the updated variable values, and test the loop termination condition only at the specified step.

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.