AP CSA Unit 6: Arrays

286 multiple-choice questions on AP Computer Science A Unit 6, Arrays, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 36 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 6.1 Creating and accessing arrays
  2. 6.2 Traversing arrays
  3. 6.3 Standard array algorithms

Key terms

6.1 Creating and accessing arrays

Array
A fixed-size collection of elements of the same type, accessed by index
Array creation expression
An expression of the form new type[length] that allocates a new array and returns a reference to it
Array element
A single value stored at one index of an array
Array initializer
A brace-enclosed list of starting values, as in int[] a = {4, 8, 15};, which also sets the array length
ArrayIndexOutOfBoundsException
Run-time error from using an index below 0 or at least length
Automatic initialization
The filling of a new array's elements with 0, 0.0, false, or null when the array is created with new
Component type
The type of each element in the array, written before the brackets, such as int in int[]
Default element values
0 for int, 0.0 for double, false for boolean, null for objects
Empty array
An array whose length is 0 and that therefore has no valid indexes
Index expression
Any int expression used in brackets to select an element, including a variable or a.length minus 1
Initializer list
The list of values inside braces that both creates an array and fills its slots in order
Last valid index
The index of the final element, equal to arr.length minus 1
length (array)
The number of elements in an array, written arr.length with no parentheses
Shared array reference
Two variables that refer to the same array object, so a change through one is visible through the other

6.2 Traversing arrays

Backward traversal
Visiting elements from the last valid index down to 0
Bounds-safe loop
An index loop whose condition keeps the index inside 0 through length minus 1, typically i < arr.length
Element copy
The value held by the enhanced-for variable, which is a copy of the array slot rather than the slot itself
Enhanced for loop
for (type x : arr) visits each element in order
Enhanced-for variable
The loop variable in for (type x : arr), which holds a copy of each element in turn
Forward index loop
An indexed loop that starts at 0 and increases until it reaches the array's length
Indexed traversal
A loop that visits elements by counting an index, usually from 0 up to length minus 1, or the reverse
Parallel traversal
Visiting corresponding elements of two arrays by using the same index in both, as in a[i] and b[i]
Partial traversal
A loop that visits only some of an array's elements, such as a range of indexes or every other slot
Traversal
Visiting each element of a collection, usually with a loop

6.3 Standard array algorithms

Adjacent pair
Two elements that sit next to each other, at indexes i and i + 1
Array rotation
A shift that saves the element that would have been overwritten and writes it into the vacated end
Element swap
Exchanging the values at two indexes by using a temporary variable
Floating-point average
The mean of array values computed by casting the sum to double before dividing by the length
In-place reverse
Rearranging an array so its elements run in the opposite order, by swapping from both ends toward the middle
Index of maximum
The position of the largest value in the array, often tracked in a variable while the array is scanned
Index of minimum
The position of the smallest value in the array, often tracked in a variable while the array is scanned
Left shift
Moving each element one index lower, so the original first value is lost and the last slot is left as a leftover duplicate
Match count
The number of elements that satisfy a condition, accumulated in a counter as the array is scanned
Nondecreasing order
An order in which each element is less than or equal to the next, so equal neighbors are allowed
Presence flag
A boolean that starts false and is set true when a target value is found in the array
Right shift
Moving each element one index higher, so the original last value is lost and the first slot is left as a leftover duplicate

Sample practice questions

  1. Which statement creates an array that can hold 10 ints?

    1. int[] nums = new int[10];
    2. int nums = new int[10];
    3. int[] nums = int[10];
    4. int[10] nums = new int[];

    Answer: A. int[] nums = new int[10]; The type int[] declares an array; new int[10] creates it with 10 elements.

  2. What does this code compute? int s = 0; for (int i = 0; i < arr.length; i++) s += arr[i];

    1. The sum of all elements in arr
    2. The largest element in arr
    3. The number of elements in arr
    4. The average of arr

    Answer: A. The sum of all elements in arr It adds each element to s.

  3. Which code finds the maximum value in a non-empty int array arr?

    1. int max = arr[0]; for (int x : arr) if (x > max) max = x;
    2. int max = 0; for (int x : arr) if (x < max) max = x;
    3. int max = arr[0]; for (int x : arr) if (x < max) max = x;
    4. int max = arr.length; for (int x : arr) if (x > max) max = x;

    Answer: A. int max = arr[0]; for (int x : arr) if (x > max) max = x; Start with the first element. Starting at 0 would fail for arrays of all negative numbers.

  4. After int[] a = new int[5];, what is the value of a[2]?

    1. null
    2. 2
    3. It is undefined and causes an error
    4. 0

    Answer: D. 0 Elements of a new int array are set to 0.

  5. Which enhanced for loop prints every element of int[] arr?

    1. for (int x : arr) System.out.print(x);
    2. for (x : arr) System.out.print(x);
    3. for (int x = arr) System.out.print(x);
    4. for (int i : arr.length) System.out.print(i);

    Answer: A. for (int x : arr) System.out.print(x); The enhanced for loop gives each element in turn to the variable x.

  6. Why is int max = 0; a poor way to start a search for the maximum?

    1. 0 isn't a valid starting value for an int variable in Java
    2. It causes a compile-time error, since max must start at null
    3. It only works for arrays of doubles, not arrays of ints
    4. If every element is negative, the result would wrongly be 0

    Answer: D. If every element is negative, the result would wrongly be 0 Initializing with arr[0] or Integer.MIN_VALUE handles all values.

  7. Given int[] a = {4, 8, 15, 16, 23, 42};, what is a[a.length - 1]?

    1. 23
    2. 6
    3. An ArrayIndexOutOfBoundsException
    4. 42

    Answer: D. 42 a.length is 6, so the last index is 5, which holds 42.

  8. What happens? int[] arr = {1, 2, 3}; for (int x : arr) x = x * 2;

    1. Every element in the array is doubled in place
    2. The array is unchanged, because x is a copy of each element
    3. Only the first element of the array is doubled
    4. A compile-time error occurs, because x can't be assigned

    Answer: B. The array is unchanged, because x is a copy of each element Assigning to the enhanced-for variable changes only the copy. Use an indexed loop to modify elements.

  9. What does this code, which swaps from both ends, do? for (int i = 0; i < arr.length / 2; i++) { int t = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = t; }

    1. Sorts the array
    2. Doubles each element
    3. Reverses the array in place
    4. Shifts every element left one position

    Answer: C. Reverses the array in place It swaps pairs from both ends moving inward.

  10. Accessing a[a.length] causes

    1. the last element to be returned
    2. the value 0 to be returned
    3. an ArrayIndexOutOfBoundsException
    4. a compile-time error

    Answer: C. an ArrayIndexOutOfBoundsException Valid indices are 0 to a.length − 1.

Practice all 286 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.