Problem of the Day
Tuesday, March 3, 2026
Problem:
Consider the following method.
/**
* Checks to see if a 2-dimensional Array is square
* @param grid a 2-dimensional array
* @return true if the array is square (with equal rows and cols)
*/
public static boolean isSquare(int[][] grid)
{
/* missing code */
}
Which of the following could be used to replace /* missing code */ so that the method will work as intended?
return grid.length - grid[0].length == 0;
if (grid.length == grid[0].length)
return true;
else
return false;return grid.length / grid[0].length == 1;
- I only
- II only
- III only
- I and II only
- I and III only
The correct answer is d. I and II both check to see if the two dimensions are equal with a correct strategy, but strategy III won't work in certain cases. For example, if grid.length is 5 and grid[0].length is 3, the integer division 5/3 will produce a result of 1 for a 5×3 grid that is not square.