FRQ 4 on the 2024 AP Computer Science A exam focuses on traversing a 2D array (grid
) to determine successive locations and compute the sum along a path. It is scored out of 9 points: Part (a) “getNextLoc” is worth 3 points, and Part (b) “sumPath” is worth 6 points (AP Central). A maximum of 3 penalty points may be assessed per question, but only in parts that have earned credit (AP Central).
Penalty points (1 point each) apply only in parts that have earned credit, up to 3 per question (AP Central). Common 1-point penalties include:
[]
vs. get()
) (AP Central)No penalties for unambiguous spelling/case issues, missing semicolons/braces when intent is clear, or extraneous non–side-effect code (AP Central).
public Location getNextLoc(int row, int col)
(3 points)Point | Criterion |
---|---|
1 | Guards against out-of-bounds access for any returned Location . |
2 | Accesses both grid[row][col+1] and grid[row+1][col] (right and below elements). |
3 | Returns the correct new Location in all four cases (bottom row wrap, rightmost column wrap, smaller of the two neighbors). |
Location
returned has valid row
and col
indices within grid
bounds (AP Central).grid[row][col+1]
and grid[row+1][col]
to compare values (AP Central).Location
: Depending on whether at bottom row, rightmost column, or which neighbor is smaller, the method must return the appropriately constructed Location
object with exactly one of the four cases handled (AP Central).Total for part (a): 3 points.
public int sumPath(int row, int col)
(6 points)Point | Criterion |
---|---|
4 | Initializes and increments a sum variable to accumulate grid values. |
5 | Iteratively determines the path by repeatedly calling getNextLoc while not at the bottom-right corner (no bounds errors). |
6 | Calls getNextLoc within the context of a loop to advance the current position. |
7 | Calls getRow() and getCol() on the returned Location object to update row and col . |
8 | Accesses grid[row][col] at positions derived from successive getNextLoc calls. |
9 | Computes the sum of all visited grid values including the first and last positions and returns that total. |
sum
: Must declare int sum = 0;
(or equivalent in recursion) and add each visited grid element to sum
(AP Central).(row < grid.length – 1 || col < grid[0].length – 1)
to visit all positions up to bottom-right without out-of-bounds access (AP Central).getNextLoc
: Each iteration must invoke getNextLoc(row, col)
on this
(use of this
optional) with two int
parameters (AP Central).getRow()
/getCol()
: After obtaining a Location
, the code must call loc.getRow()
and loc.getCol()
to update row
and col
(AP Central).grid[row][col]
based on the updated coordinates each time (AP Central).grid
values along the path, including both starting and ending cells; failing to include either endpoint loses credit (AP Central).Total for part (b): 6 points. Total for question 4: 9 points.