ITPEC FE Subject B October 2025 Question 14
From the answer group below, select the correct answer to be inserted into blank in the program. Here, the array index starts at 1.
A 3 × 3 normal magic square is a 3 × 3 matrix where the sum of the elements for each row, each column, and each diagonal is the same. The square contains the numbers 1 to 9, exactly as shown in the figure.
Figure 3 × 3 normal magic square
The function checkMagicSquare receives a two-dimensional 3 × 3 integer array (matrix) m containing the numbers 1 to 9, and returns whether the given matrix is a magic square or not.
Answer group
Explanation
(rowSum[k] ≠ colSum[k]) or (rowSum[k] ≠ dia1Sum) — reject the matrix as soon as any row or column sum differs from the diagonal sum.
The first loop builds rowSum[i] (the sum of row i), colSum[i] (the sum of column i, from m[j,i]) and both diagonal sums. Once dia1Sum ≠ dia2Sum has been ruled out, the matrix is a magic square exactly when rowSum[k] = colSum[k] = dia1Sum for every k.
The blank sits in front of return false, so it must be the negation of that requirement: not (r = c and r = d), which by De Morgan's law is r ≠ c or r ≠ d. One failed equality is enough to reject.
For the figure's square every row, column and diagonal sums to 15, so for k = 1 to 3 both comparisons are equal, the condition stays false and the function returns true.
Answer (d)
Why not others:
- •(a) and (b) — the condition is true when the sums are equal, so the figure's square, all 15, is rejected at k = 1
- •(c) —
andrejects only when both comparisons fail at once. The matrix with rows 1 2 5, 3 9 6 and 4 7 8 has both diagonals equal to 18 and each row sum equal to its column sum (8, 18, 19), so no k triggers the condition and a non-magic square is accepted
Key rule: The guard in front of return false is the negation of the success condition, and negating an and of equalities gives an or of inequalities (De Morgan).
Array Manipulation · 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 FE past-paper collection or Report an issue.