The fourth FRQ on the AP CSA Exam as of 2026 will be on 2D Arrays and worth 6 points. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze,and manipulate data in an 2D array structure.
Here are two common nested loop patterns for nested loops that you will need to use in this FRQ. In the patterns below, the outer loop iterates through the rows, and the inner loop iterates through the columns. Use enhanced for-each loops if indices are not needed. Use for-loops with row and col indices if you need to access specific elements of the 2D array or to return an index. This type of loop can loop through rows first or columns first if needed.
The Appointment class is used to store information about a personβs scheduled appointments. A partial declaration of the Appointment class is shown below.
public class Appointment
{
/**
* Returns the status of the appointment ("free", "busy", etc.)
*/
public String getStatus()
{ /* implementation not shown */ }
/**
* Returns the room number of the appointment location
*/
public int getRoomNumber()
{ /* implementation not shown */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}
The Schedule class maintains a two-dimensional array of appointments that represents a personβs schedule. A partial declaration of the Schedule class is shown below. (Note that it contains a 2D array of Appointment objects.)
public class Schedule
{
private Appointment[][] sched;
/**
* Returns the index of a column containing the fewest
* occurrences of the status indicated by the parameter
* target
* Preconditions: sched is not null and no elements
* of sched are null.
* sched has at least one row and at
* least one column.
*/
public int columnWithFewest(String target)
{ /* to be implemented */}
/* There may be instance variables, constructors, and methods
that are not shown. */
}
When an element of the two-dimensional array sched is accessed, the first index is used to specify the row and the second index is used to specify the column.
Write the Schedule method columnWithFewest. The method should return the index of a column in sched that contains the fewest occurrences of the parameter target. If there are multiple columns that have the fewest number of occurrences of target, any of their column indices can be returned.
The call columnWithFewest("free") should return either 2 or 3 because "free" appears one time in column 2, one time in column 3, and more than one time in each of the other columns.
/**
* Returns the index of a column containing the fewest
* occurrences of the status indicated by the parameter
* target
* Preconditions: sched is not null and no elements
* of sched are null.
* sched has at least one row and at
* least one column.
*/
public int columnWithFewest(String target)
Which of the following types of algorithms do we need to use to solve the problem of finding the index of a column in the sched 2D array that contains the fewest occurrences of the target status?
Which of the following loops is the most appropriate to use for the outer loop in the columnWithFewest method to return the index of a column in the sched 2D array that contains the fewest occurrences of the target status?
Notice that the method needs to return an index, so we cannot use an enhanced for-each loop which does not provide an index. Instead, we need to use a traditional for loop with an index variable.
for (int row = 0; row < sched.length; row++)
This is incorrect because we need to iterate through columns first, not rows.
for (int col = 0; col < sched.length; col++)
This is close, but sched.length refers to the number of rows, not columns.
for (int col = 0; col < sched[0].length; col++)
This is correct because sched[0].length refers to the number of columns in the first row.
While looping through the columns and rows of the 2D array sched, which of the following expressions would test to see if the status of the appointment at a given row and col is equal to the target?
This is incorrect because it compares the entire Appointment object to a String.
sched[row][col].getStatus() == target
This is incorrect because it uses the β==β operator, which checks for reference equality, not value equality.
sched[row][col].getStatus().equals(target)
Yes, this is correct because it uses the .equals() method to check for value equality between the status and target.
sched[row][col].status == target.status
This is incorrect because it assumes that status is a public variable, which it is not in this case. Use the getStatus() method given to you in the problem definition.
The following AP rubric is used to grade the columnWithFewest method in the Schedule class. Each item is worth 1 point, for a total of 6 points. Did you receive all of the points? In class, your teacher may have you grade each othersβ code.