ITPEC IP October 2024 Question 8

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

ITPEC IP October 2024 — Question 8 of 100

At the start of each output row, set k to 1; inside the inner loop, increment k by 1. The required statements are k ← 1 and k ← k + 1. The outer variable i is the current row length. Resetting k to 1 makes every row begin with 1. The inner loop then outputs k and increments it until the values 1 through i have been printed.

After the line break, the existing i ← i + 1 advances to the next row. With n = 6, the row lengths become 1, 2, 3, 4, 5, and 6.

Answer (b)

Why not others:
- (a) changes i inside the inner loop and leaves k uninitialized

- (c) also fails to initialize and advance k correctly

- (d) derives k from old values without resetting it to 1 for each row

Key rule: Nested-loop pattern generation normally resets the inner counter before each inner loop and advances that counter once per emitted item.

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.