ITPEC IP April 2025 Question 7
A program uses loops to display results based on whether the cycle numbers are odd or even. The execution result of the program is shown below.
Which of the following is an appropriate combination of pieces of code to be inserted into A through C in the program?
Explanation
A: (i mod 2 = 0) and (j mod 2 = 0), B: i, C: "+" — a digit appears only where both loop counters are even; everything else is a plus sign.
The outer loop gives the row (i = 0 to 6) and the inner loop the column (j = 0 to 6), seven characters per row:
- •Even rows (
i= 0, 2, 4, 6): the digitiin the even columns and+in the odd ones, as in "0+0+0+0" - •Odd rows (
i= 1, 3, 5): seven plus signs, "+++++++"
So the condition is true exactly when i and j are both even, and the true branch outputs i, the false branch "+".
Answer (d)
Why not others:
- •(a) — odd/odd test with
"+"when true: the first row would be "0000000" and the second "1+1+1+1" - •(b) — odd/odd test with
iwhen true: digits would appear only in odd rows, as "+1+1+1+", and the first row would be all plus signs - •(c) — right condition, outputs swapped: the first row would be "+0+0+0+" and the odd rows "1111111"
Key rule: With nested loops, map the outer counter to rows and the inner one to columns, then test one row of each kind against the condition.
Algorithm and Programming · AI-written, may contain errors
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.