ITPEC FE Subject B October 2025 Question 14
ITPEC FE Subject B October 2025 — Question 14 of 20
Filling in the condition to validate a 3×3 normal magic square
The program computes rowSum[k], colSum[k], dia1Sum, and dia2Sum in the first loop, then checks dia1Sum ≠ dia2Sum to reject early. The blank is the guard inside the final for k loop that must reject the matrix if any row or column sum deviates.
- •The
iftriggersreturn false, so it must catch invalid cases - •A magic square requires
rowSum[k] = colSum[k] = dia1Sumfor everyk - •Invalidity means at least one equality fails → use
or(disjunction)
| Option | Condition | Verdict |
|---|---|---|
a) = and = | Checks both match → would reject valid squares | Wrong |
b) = or = | Checks at least one matches → rejects almost everything | Wrong |
c) ≠ and ≠ | Both must fail simultaneously → misses cases where only one fails | Wrong |
d) ≠ or ≠ | At least one fails → correctly catches any deviation | Correct |
Why not others:
- a) and b) use equality checks (=), meaning the if fires when sums are equal — the opposite of what we want
- c) uses and with inequality, so it only catches the case where both row≠col and row≠diagonal simultaneously; a row matching the column but not the diagonal would slip through
Key rule: When guarding against invalidity, use ≠ ... or ≠ ... (reject if any condition fails). Use ≠ ... and ≠ ... only when you need all conditions to fail simultaneously.
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 FE past-paper collection or Report an issue.