ITPEC IP April 2026 Question 9
A hollow square is an n × n matrix whose first and last rows along with the first and last columns are filled with stars (“*”), where n denotes an integer. All other positions inside the matrix other than the borders are filled with spaces. If n is 5, the hollow square is shown as below.
The procedure printHollowSquare prints a hollow square based on the value of n. Which of the following is the correct answer to be inserted into blank in the program? Here, the value n is at least 3 and is passed as an argument to the procedure.
Explanation
i = 1 or i = n or j = 1 or j = n — a position is on the border when it is in the first or last row, or in the first or last column.
Row i and column j both run from 1 to n. The top and bottom rows are i = 1 and i = n, the left and right columns j = 1 and j = n. A star belongs wherever any one of the four holds, so they are joined with or.
Check with n = 5:
- •Row 1:
i = 1is true for everyj, so five stars - •Row 3: only
j = 1andj = 5make the condition true, so a star, three spaces, a star - •Row 5:
i = nis true for everyj, so five stars
This is the figure.
Answer (c)
Why not others:
- •(a) — joins the row test and the column test with
and, so a star needs both a border row and a border column: only the four corners - •(b) — true whenever the row or the column is an inner one, so it prints stars everywhere except the four corners
- •(d) — true only for the inner positions, the exact opposite: stars inside, spaces on the border
Key rule: A border is "not inside": by De Morgan's law NOT(i > 1 and i < n and j > 1 and j < n) becomes i ≤ 1 or i ≥ n or j ≤ 1 or j ≥ n, which within 1 to n is option (c).
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.