Skip to main content

Question 4: 2D Array

Crop totals for each column of a garden

  • Unit 4
  • 6 points
  • About 22 minutes

You write one method that traverses, analyzes or changes data in a 2D array, often of objects, using the classes described in the question. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 6 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 4 of 4, 6 points. Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.

The question and its sources

A school garden is divided into a grid of plots. Each plot is represented by a Plot object that knows its crop and how many kilograms it produced. The Garden class stores the plots in a two-dimensional array. When an element of a two-dimensional array is accessed, the first index is the row and the second index is the column.

The Plot class

Java
public class Plot
{
    /** Returns the name of the crop in this plot, such as "beans" */
    public String getCrop()
    { /* implementation not shown */ }
 
    /** Returns the harvest from this plot, in whole kilograms (at least 0) */
    public int getYield()
    { /* implementation not shown */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

The Garden class

Java
public class Garden
{
    /**
     * The plots. plots has at least one row and at least one column,
     * every row has the same length, and there are no null elements.
     */
    private Plot[][] plots;
 
    /**
     * Returns an array with one element for each column of plots, as
     * described in the question.
     * Postcondition: plots is unchanged.
     */
    public int[] columnTotals(String crop)
    { /* to be implemented */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example: plots, written as crop/yield

RowColumn 0Column 1Column 2Column 3
0beans/4kale/2beans/1corn/9
1kale/3beans/5beans/2kale/1
2beans/6corn/8kale/4kale/2

Source: Hypothetical example

Example calls on the plots above

CallValue returned
columnTotals("beans"){10, 5, 3, 0}
columnTotals("kale"){3, 2, 4, 3}
columnTotals("peas"){0, 0, 0, 0}

Source: Hypothetical example

Suggested time: 22 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

6 points

Write the Garden method columnTotals. It returns a new int array with one element for each column of plots. Element c of the returned array is the total yield of the plots in column c whose crop is crop, or 0 if no plot in that column has that crop. For example, element 0 of columnTotals("beans") is 10, because column 0 has beans plots with yields 4 and 6. Complete method columnTotals.public int[] columnTotals(String crop)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Checking scoring…

Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.