AP CSA FRQ Guide

2018 FRQ 2: WordPairList

Write the WordPairList constructor and a matching-count method. The constructor builds every ordered pair of words where the second word appears later in the array. The method counts pairs whose two strings match.

ArrayList Construction and Object Traversal22 minute targetArrayListnested loopsobjectsString equalsconstructors

Skills Tested

What this FRQ is really practicing

ArrayListnested loopsobjectsString equalsconstructors

Treat these skills as the study checklist. If any tag feels shaky, review that topic before attempting the full written response.

Starter Approach

How to begin without copying a solution

  1. 1Constructor: initialize allPairs to a new ArrayList of WordPair objects.
  2. 2Use nested loops so the first index comes before the second index.
  3. 3Create each WordPair exactly once and add it to allPairs.
  4. 4numMatches: traverse allPairs.
  5. 5Use getFirst and getSecond to compare the two strings in each pair.
  6. 6Use equals for String comparison and return the number of matches.

Write a first attempt before revealing any solution outline. Most AP CSA FRQ progress comes from tracing your own code and finding the missing case.

Common Mistakes

Mistakes to watch for while writing

Pairing each word with itself.
Generating both (i, j) and (j, i) instead of only later-word pairs.
Forgetting to initialize allPairs.
Using == for String comparison.
Returning after the first match instead of counting every match.

Self Check

Review questions before you submit

Defines the WordPairList constructor with a String[] parameter.

The constructor receives the source word array.

Initializes allPairs to a new ArrayList.

The instance variable must be created before adding pairs.

Uses an outer loop over earlier word indexes.

The first index should start near 0 and leave room for a later word.

Uses an inner loop whose index starts after the outer index.

Pair each word only with words that appear later.

Creates WordPair objects from two array entries.

The constructor should call new WordPair with words[i] and words[j].

Adds each new WordPair to allPairs.

The list should contain all generated pairs.

Defines numMatches with int return type and no parameters.

The method returns the count of matching pairs.

Traverses allPairs.

Use an indexed loop or enhanced for loop over the list.

Practice Links

Where to go next