Unit 6: Array

AP Computer Science A: 51 practice questions with detailed explanations.

Unit Study Guide

Executive Summary

Arrays store many values of one type in a fixed-size, indexed structure. Indexes run 0 to length - 1.

Creating and accessing

int[] a = new int[n] creates n zeros. Access with a[i]; the length is a.length (a field, not a method). Indexing past the end throws ArrayIndexOutOfBoundsException at runtime, not compile time.

Traversals

Use for (int i = 0; i < a.length; i++) for full access to indexes. The enhanced for (int x : a) reads values only — x is a copy, so assigning to it does not change the array.

Common algorithms

Sum with an accumulator. Find max by tracking the largest value (and its index). Count matches with a counter. Reverse by swapping mirror pairs through the middle. Search linearly with a flag or early return; return -1 when not found.

Aliasing

After int[] b = a, both names point to the same array — changing b[0] changes a[0]. To copy values, loop them over.

Exam traps

The last index is a.length - 1. A loop with i ≤ a.length crashes on the final iteration. Enhanced for loops cannot modify elements or traverse in reverse.

Top 5 Concepts to Master

  1. 1Declare and initialize arrays.
  2. 2Traverse with index loops and enhanced for.
  3. 3Implement sum, max, count, and reverse algorithms.
  4. 4Trace aliased arrays.

Key Terms & Definitions

Practice with Flashcards
Array

Fixed-size indexed collection of one type.

Index

Position in an array, starting at 0.

length

Public field holding the array’s size.

ArrayIndexOutOfBoundsException

Runtime error for an invalid index.

Enhanced for loop

For-each loop that copies each element.

Traversal

Visiting every element of a structure.

Common Misconceptions: Exam Traps

Assigning to the for-each variable changes the array.

Correct: The variable is a copy of each element.

a.length is a method call.

Correct: length is a field — no parentheses.

An array variable copy duplicates the data.

Correct: Both references share the same array.

Out-of-bounds access fails at compile time.

Correct: It compiles and throws at runtime.

Question Bank Breakdown

By difficulty

easy 21medium 30

By topic

Array Creation and Access 22Array Algorithms 13Traversing Arrays 8Enhanced for Loop 8

All Questions in this Unit