AP CSA 2017 FRQ 4 Position
AP CSA 2017 FRQ 4: Position
Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF
| Question Type | 2D Array Operations |
| Key Skills | Write findPosition() (search a 2D String array for a target and return its Position object, or null if not found) and al |
| Difficulty | Hard |
| AP CSA Units | Unit 4: Data Collections — 2D array traversal with nested loops, ArrayList construction, returning objects. |
What This Problem Asks
Write findPosition() (search a 2D String array for a target and return its Position object, or null if not found) and allPositions() (return an ArrayList of all positions where the target appears).
Official Question & PDF
PDF cannot display here. Open PDF directly →
Provided Code
public class Position
{
public Position(int r, int c) {
/* given */
}
public int getRow() {
/* given */
}
public int getCol() {
/* given */
}
}
// Methods to implement are in a separate class (not shown):
public static Position findPosition(String str, String[][] grid)
{ /* to be implemented in part (a) */ }
public static ArrayList allPositions(String str, String[][] grid)
{ /* to be implemented in part (b) */ }
Part A — findPosition (5 Points)
Write findPosition(). Search the 2D array grid for str. Return a new Position(row, col) for the first match found (in row-major order), or null if str is not in grid.
Scoring Rubric
| +1 | Nested loop traverses all rows and columns |
| +1 | Uses .equals() to compare strings (not ==) |
| +1 | Returns new Position(row, col) on match |
| +1 | Returns null when no match found |
| +1 | Correct traversal algorithm (row-major) |
Solution
public static Position findPosition(String str, String[][] grid)
{
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[r].length; c++)
{
if (grid[r][c].equals(str))
{
return new Position(r, c);
}
}
}
return null;
}
Part B — allPositions (4 Points)
Write allPositions(). Return an ArrayList containing a Position object for every location where str appears in grid. The list may be empty if str does not appear.
Scoring Rubric
| +1 | Creates an ArrayList to collect results |
| +1 | Traverses entire grid (does not stop early) |
| +1 | Adds a new Position for each match |
| +1 | Returns the complete list (algorithm) |
Solution
public static ArrayListallPositions(String str, String[][] grid) { ArrayList result = new ArrayList (); for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if (grid[r][c].equals(str)) { result.add(new Position(r, c)); } } } return result; }
Common Mistakes to Avoid
Wrong
if (grid[r][c] == str) // unreliable
Correct
if (grid[r][c].equals(str)) // correct
== compares object references; .equals() compares string content. Always use .equals() for String comparisons in Java.
Wrong
if (grid[r][c].equals(str)) {
return new Position(r,c); // only finds first
}
Correct
result.add(new Position(r, c)); // collect all, return list at end
allPositions must find ALL occurrences, not just the first. Never return early — traverse the complete grid and add every match to the list.
Exam Tips
Frequently Asked Questions
What does 2017 AP CSA FRQ 4 Position ask?
It asks students to write two static methods: findPosition() returns the Position of the first occurrence of a target string in a 2D array (or null), and allPositions() returns an ArrayList of every Position where the target appears.
What is the main difference between findPosition and allPositions?
findPosition returns immediately on the first match (and returns null if none found). allPositions traverses the entire grid, collects every match in an ArrayList, and returns the list (which may be empty) — it must never return early.
See All 2017 AP CSA FRQs
View the complete 2017 exam hub with solutions, difficulty analysis, and scoring for all four questions.
View 2017 FRQ Hub →Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]