From the free course AP Computer Science A on Lykke

AP Practice 4

A two-dimensional array is best understood as a grid: one index chooses a row, and another chooses a position within that row. On an AP Computer Science A free-response question, success depends less on writing a clever algorithm than on translating the grid’s meaning into precise traversal, selection, and return…

AP Practice 4

A two-dimensional array is best understood as a grid: one index chooses a row, and another chooses a position within that row. On an AP Computer Science A free-response question, success depends less on writing a clever algorithm than on translating the grid’s meaning into precise traversal, selection, and return logic.

Task type: 2D-array method implementation

This original, unofficial practice task targets the 2D-array free-response task type. It primarily assesses Computational Thinking Practice 2: Develop Code, while also requiring Computational Thinking Practice 1: Design Code, Computational Thinking Practice 3: Analyze Code, and Computational Thinking Practice 4: Document Code and Computing Systems.

Original prompt

A wildlife station records the number of animals observed in several habitat zones during several observation periods. The observations are stored in a rectangular two-dimensional array, where observations[r][c] represents the count recorded for habitat row r during period column c.

Write a method countActiveZones that returns the number of rows containing at least one observation greater than threshold.

public static int countActiveZones(int[][] observations, int threshold)

For example, given the following array and a threshold of 10:

int[][] observations = {
    {4, 7, 9},
    {12, 3, 5},
    {2, 11, 14},
    {6, 8, 10}
};

the method should return 2, because the second row contains 12, and the third row contains 11 and 14. The value 10 does not count because the requirement is greater than the threshold.

Design before coding

The phrase “number of rows containing at least one qualifying value” determines the algorithm. The outer loop examines one row at a time; the inner loop searches that row. Once a qualifying value is found, the row should be counted once and the inner loop can stop.

Requirement Implementation decision
Examine every row Outer loop from 0 through observations.length - 1
Examine values in the current row Inner loop through observations[r].length
Qualifying observation observations[r][c] > threshold
Count a row only once Boolean flag or immediate break
Return the total Return the row counter

The critical distinction is between counting qualifying values and counting qualifying rows. If one row contains three values above the threshold, it still contributes only 1 to the result.

Worked solution

public static int countActiveZones(int[][] observations, int threshold)
{
    int activeZones = 0;

    for (int r = 0; r < observations.length; r++)
    {
        boolean hasQualifyingObservation = false;

        for (int c = 0; c < observations[r].length; c++)
        {
            if (observations[r][c] > threshold)
            {
                hasQualifyingObservation = true;
                break;
            }
        }

        if (hasQualifyingObservation)
        {
            activeZones++;
        }
    }

    return activeZones;
}

Trace the example row by row:

Row Values above $10$? Adds to activeZones?
$0$ No No
$1$ $12$ Yes; total $1$
$2$ $11$, $14$ Yes; total $2$
$3$ No; $10$ is not greater than $10$ No

The final return value is $2$. The break is safe because the method cares only whether the current row has at least one qualifying value. Continuing through the rest of that row cannot change the answer for that row.

What earns credit

A strong response should visibly satisfy these scoring ideas:

  • Use the parameter representing the two-dimensional array rather than replacing it with unrelated data.
  • Traverse rows and columns with valid bounds.
  • Access an individual element using both indices: observations[r][c].
  • Apply the required strict comparison: > threshold.
  • Ensure that each qualifying row contributes exactly one count.
  • Return the computed integer.

These criteria reward Develop Code because the method must be implemented, Analyze Code because the result must be traced against the data, and Document Code and Computing Systems because the code’s behavior should be explainable from its conditions and structure.

Misconception check: rectangular arrays

A common error is writing the inner-loop bound as observations.length. That expression gives the number of rows, not the number of values in the current row. The safer bound is observations[r].length, because Java represents a two-dimensional array as an array whose elements are themselves arrays.

Another common error is incrementing activeZones inside the inner loop. That would count qualifying observations rather than qualifying rows. The flag-and-break pattern separates the two questions: “Did this row qualify?” and “How many rows qualified?”

Timing and error review

For a timed 2D-array method, reserve approximately $2$ minutes to identify the counted object, $6$–8$ minutes to write and trace the method, and $2$ minutes to test boundary cases. Check an empty array, a row with no qualifying values, a row with several qualifying values, and a value exactly equal to the threshold.

Afterward, classify every mistake: index error, boundary error, condition error, wrong quantity counted, or return-value error. Then write one corrected sentence—for example, “I must count rows, so I increment only after the inner search establishes that the row qualifies.”

Retrieval check

If a row contains five values greater than the threshold, how much should it increase the result? Why must the comparison use > rather than >= when the requirement says “greater than”?

AP Practice 4 - AP Computer Science A - image 1
AP Practice 4 - AP Computer Science A - image 1
AP Practice 4 - AP Computer Science A - diagram 1
AP Practice 4 - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis