Question 4: 2D Array
Finding a block of empty seats
- 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 theater's booking system stores its seats in a two-dimensional array of Seat objects. Each row of the array is one row of seats, from left to right. When an element of a two-dimensional array is accessed, the first index is the row and the second index is the column.
The Seat class
public class Seat
{
/** Returns true if someone has booked this seat; false otherwise */
public boolean isTaken()
{ /* implementation not shown */ }
// There may be instance variables, constructors, and methods that are not shown.
}The SeatingChart class
public class SeatingChart
{
/**
* The seats in the theater. seats has at least one row and at least
* one column, and contains no null elements.
*/
private Seat[][] seats;
/**
* Returns the index of the first row that has at least k empty seats
* next to each other, or -1 if no row does, as described in the
* question.
* Precondition: k > 0
*/
public int firstRowWithBlock(int k)
{ /* to be implemented */ }
// There may be instance variables, constructors, and methods that are not shown.
}Example: seats ("taken" means isTaken() returns true)
| Row | Column 0 | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 |
|---|---|---|---|---|---|---|
| 0 | taken | taken | empty | taken | empty | empty |
| 1 | empty | taken | empty | empty | empty | taken |
| 2 | empty | empty | empty | empty | taken | taken |
| 3 | taken | taken | taken | taken | taken | taken |
Source: Hypothetical example
Example calls on the seats above
| Call | Value returned | Why |
|---|---|---|
firstRowWithBlock(2) | 0 | Row 0 has 2 empty seats next to each other (columns 4 and 5). |
firstRowWithBlock(3) | 1 | Row 1 has 3 (columns 2–4). Row 0 has at most 2. |
firstRowWithBlock(4) | 2 | Row 2 has 4 (columns 0–3). |
firstRowWithBlock(5) | -1 | No row has 5 empty seats next to each other. |
Source: Hypothetical example
Suggested time: 22 minutes
Your answers are saved in this browser as you type.
Part (a)
6 pointsWrite the SeatingChart method firstRowWithBlock. Seats next to each other must be in the same row: empty seats at the end of one row and the start of the next row do not form a block.
Complete method firstRowWithBlock.public int firstRowWithBlock(int k)
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.