AP CSA FRQ Guide

2021 FRQ 4: ArrayResizer

Write two static methods for resizing a 2D array. The first determines whether one row has no zero values. The second creates a new 2D array containing only rows that have no zeros, preserving row order.

2D Arrays and Row Filtering22 minute target2D arraysstatic methodsrow traversalcopying arrayshelper methods

Skills Tested

What this FRQ is really practicing

2D arraysstatic methodsrow traversalcopying arrayshelper methods

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. 1Part A: examine every column in row r and return false if any value is zero.
  2. 2Return true only when all values in the row are nonzero.
  3. 3Part B: use numNonZeroRows to create a result array with the correct number of rows.
  4. 4Copy only rows that pass isNonZeroRow, preserving their original order.
  5. 5Return the resized 2D array without changing array2D.

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

Checking whether a row contains at least one nonzero value instead of requiring all values to be nonzero.
Sizing the result array with the original row count instead of numNonZeroRows.
Incrementing the result-row index for rows that were not copied.
Changing array2D even though the postcondition says it is unchanged.
Copying columns with array2D.length instead of array2D[0].length.

Self Check

Review questions before you submit

Defines isNonZeroRow as a public static boolean method.

Part A is a static helper method with int[][] and row index parameters.

Traverses every column in row r.

Use array2D[0].length or array2D[r].length.

Returns false when a zero is found in row r.

Check array2D[r][col] == 0.

Returns true after the row is fully checked.

The true return belongs after the loop.

Defines resize as a public static method returning int[][].

Part B returns a new 2D int array.

Uses numNonZeroRows to size the result array.

The helper tells how many rows the new array needs.

Creates a new 2D array with original column count.

Use new int[numNonZeroRows(array2D)][array2D[0].length].

Calls isNonZeroRow to decide which rows to copy.

Part B should use the Part A helper.

Practice Links

Where to go next