AP Computer Science A · Exam guide

AP Computer Science A Question 4 Explained: Working with a 2D Array

Question 4 asks you to process a two-dimensional array. It is worth 9 points, and the main skill is keeping rows and columns straight. The example below was compiled and run.

A sample question

Write columnSums(int[][] grid), which returns a new array whose element c is the sum of column c of grid, and largestRowSum(int[][] grid), which returns the largest sum of any row. Assume the grid is rectangular and has at least one row.

A full-credit solution

public static int[] columnSums(int[][] grid)
{
    int[] sums = new int[grid[0].length];
    for (int r = 0; r < grid.length; r++)
    {
        for (int c = 0; c < grid[0].length; c++)
        {
            sums[c] += grid[r][c];
        }
    }
    return sums;
}

public static int largestRowSum(int[][] grid)
{
    int best = Integer.MIN_VALUE;
    for (int[] row : grid)
    {
        int sum = 0;
        for (int v : row)
        {
            sum += v;
        }
        if (sum > best)
        {
            best = sum;
        }
    }
    return best;
}

Tested with the grid {{1, 2, 3}, {4, 5, 6}}: columnSums returns 5, 7, 9, and largestRowSum returns 15.

Reading a 2D array

ExpressionMeaning
grid.lengthThe number of rows.
grid[0].lengthThe number of columns, when the grid is rectangular.
grid[r][c]The element in row r and column c, with the row first.
for (int[] row : grid)Visits each row as a one-dimensional array.

How the points are earned

PieceWhere it appears
Declares and initializesint[] sums = new int[grid[0].length]; and int best = Integer.MIN_VALUE;
Loops over the correct rows and columnsr and c stop before grid.length and grid[0].length
Uses grid[r][c] with the row firstsums[c] += grid[r][c];
Updates the right variablethe column sum goes into sums[c], and the best sum is updated only when it is larger
Returns the right type at the right timethe array and the int are returned after the loops
RowPointsWhat earns it
Correctness of the algorithm6Six one-point pieces: declares the needed variables; loops over the correct range; tests the stated condition; updates the right variable on each pass; handles edge cases; returns the right value at the right time.
Use of the Java API and syntax2One point for using the given methods and the ArrayList and array syntax correctly; one for Java that would compile, with matching headers and declared variables.
Program design and encapsulation1Uses the class's own methods and keeps data private, as the question's design requires.
Total9

Common mistakes

  • Swapping row and column, writing grid[c][r].
  • Using grid.length for the number of columns.
  • Starting the largest-sum variable at 0, which fails when every sum is negative.
  • Returning inside a loop, so only the first row or column is processed.
  • Creating the result array with the wrong length.

Frequently asked questions

What is Question 4 about?

Processing a two-dimensional array, usually by row, by column, or both.

How do I find the number of columns?

Use grid[0].length, which is the length of the first row.

Why start the best sum at Integer.MIN_VALUE?

It is smaller than any possible sum, so the first row always replaces it, even when all sums are negative.

Can I use an enhanced for loop on a 2D array?

Yes, to visit rows, as in for (int[] row : grid). Use index loops when you need the column number.

Where can I find the official scoring rules?

Exam formats change a little from year to year, so check the current course and exam description on the College Board site before test day.

Keep practicing

The AP Computer Science A course has eight free-response prompts, two of each exam type, each with a coding space, the scoring notes, and the canonical solution explained point by point. These free lessons also build the skills the free-response section tests:

See the AP Computer Science A course Free lessons and lesson list

Other free-response guides:

AP is a registered trademark of the College Board, which is not affiliated with and does not endorse this site. This guide is our own explanation of the question types and scoring. For the official scoring guidelines, see the College Board.