Learn AP Comp Sci

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?


  1. return grid.length - grid[0].length == 0;

  2. if (grid.length == grid[0].length)
    return true;
    else
    return false;

  3. return grid.length / grid[0].length == 1;
  1. I only
  2. II only
  3. III only
  4. I and II only
  5. I and III only

Show solution:

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.