AP Computer Science A Resource

Arrays practice guide

A focused AP Computer Science A arrays guide covering indexes, traversal patterns, in-place updates, common boundary mistakes, and AP-style tracing.

Study Snapshot

AP unit

Unit 4

Exam use

Array creation, indexing, traversal, mutation, algorithms, and 2D arrays

Study time

60-75 min

Best used when

Students who need stronger confidence with zero-based indexing, loop bounds, array mutation, and code tracing.

Indexes start at 0
Last index is length - 1
Enhanced for loops read values but do not replace array elements

Array Foundations

Arrays store a fixed number of values of the same type and are accessed with zero-based indexes.

+

Use values.length for the number of elements.

+

The valid index range is 0 through values.length - 1.

+

New int arrays begin with 0 values; object arrays begin with null references.

+

An array variable stores a reference to the array object.

int[] scores = new int[4];
scores[0] = 95;
scores[1] = 88;
System.out.println(scores.length); // 4

Traversal Patterns

Most AP Computer Science A array problems are loops that inspect, count, update, or compare elements.

+

Use an indexed loop when you need positions or need to modify elements.

+

Use an enhanced for loop when you only need to read each value.

+

Stop at length - 1 when comparing an element with the next element.

+

Trace loop variables and array values after each iteration.

int total = 0;
for (int i = 0; i < values.length; i++) {
  total += values[i];
}

In-Place Updates

Array questions often test whether students understand that array elements can change during a loop.

+

Use a temporary variable when swapping two elements.

+

Be careful when a later update reads a value that was already changed.

+

Methods can mutate array elements because the array reference is passed to the method.

+

Reassigning a parameter to a new array does not change the caller variable.

int temp = data[left];
data[left] = data[right];
data[right] = temp;

2D Arrays

Two-dimensional arrays extend the same index rules to rows and columns.

+

Use grid.length for the number of rows.

+

Use grid[0].length for the number of columns in a rectangular 2D array.

+

Nested loops usually visit every row-column pair.

+

Changing a row through an enhanced for loop can still mutate the original 2D array.

for (int r = 0; r < grid.length; r++) {
  for (int c = 0; c < grid[0].length; c++) {
    System.out.print(grid[r][c]);
  }
}

Practice checklist

Use these prompts as a short self-check before moving back into FRQs or class assignments.

Reverse an array in place using two indexes and a temporary variable.
Count how many adjacent pairs are increasing.
Replace every negative value with 0 using an indexed loop.
Trace a nested loop over a 2D array and record row, column, and value.

Related Practice

Keep practicing Arrays from the right next page.

Use these links to move from the lesson into MCQ practice, FRQ writing, compiler drills, or a targeted tutoring plan.

For practice use only.

Java Arrays MCQ Quiz

Practice AP Computer Science A array tracing, loop boundaries, enhanced for loops, in-place updates, reference behavior, search/sort traces, and 2D arrays.

Question 1 of 25

Answered 0 of 25

Choose one answer.

After mystery reverses the array in place, which output is produced?

public static void mystery(int[] list) {
  for (int i = 0; i < list.length / 2; i++) {
    int j = list.length - 1 - i;
    int temp = list[i];
    list[i] = list[j];
    list[j] = temp;
  }
}

public static void main(String[] args) {
  int[] array = {1, 3, 5, 7, 9};
  mystery(array);
  for (int val : array) {
    System.out.print(val + " ");
  }
}

Need help applying this?

Bring this topic into your next AP Computer Science A session.