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.

0+0+0+0
+++++++
2+2+2+2
+++++++
4+4+4+4
+++++++
6+6+6+6

Which of the following is an appropriate combination of pieces of code to be inserted into A through C in the program?

[Program]
integer: i, j
for (increase i from 0 to 6 by 1)
for (increase j from 0 to 6 by 1)
if (A)
output B
else
output C
endif
endfor
output a new line
endfor
OptionABC

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 digit i in 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 i when 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.