AP CSA 2017 FRQ 4 Position

FRQ Archive2017 FRQs › FRQ 4: Position
2017 AP CSA • 2D Array Operations

AP CSA 2017 FRQ 4: Position

Complete solution, scoring rubric, and walkthrough — verified from the official College Board PDF

9 Points Hard 2D Array Operations Classic Format (Pre-2019)
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

Download Full 2017 FRQ 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) */ }
Timer 22:00

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.

Drag corner to expand ▽

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;
}
Return immediately on the first match in row-major order. Return null only after the nested loops complete without finding a match. Use .equals() for String comparison — never ==.

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.

Drag corner to expand ▽

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 ArrayList allPositions(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;
}
Unlike findPosition(), do NOT return early. Traverse the entire grid and add every match. Return the list at the end — it may be empty if str was not found.

Common Mistakes to Avoid

Using == instead of .equals() for String comparison

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.

Returning early in allPositions like findPosition

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

Part A returns a single Position or null. Part B returns an ArrayList (never null — an empty list if nothing found).
Use grid.length for the number of rows and grid[r].length for columns in each row.
Create a new Position object for each match: new Position(r, c).

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]