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
- 6.1 Creating and accessing arrays
- 6.2 Traversing arrays
- 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
Which statement creates an array that can hold 10 ints?
- int[] nums = new int[10];
- int nums = new int[10];
- int[] nums = int[10];
- 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.
What does this code compute? int s = 0; for (int i = 0; i < arr.length; i++) s += arr[i];
- The sum of all elements in arr
- The largest element in arr
- The number of elements in arr
- The average of arr
Answer: A. The sum of all elements in arr It adds each element to s.
Which code finds the maximum value in a non-empty int array arr?
- int max = arr[0]; for (int x : arr) if (x > max) max = x;
- int max = 0; for (int x : arr) if (x < max) max = x;
- int max = arr[0]; for (int x : arr) if (x < max) max = x;
- 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.
After int[] a = new int[5];, what is the value of a[2]?
- null
- 2
- It is undefined and causes an error
- 0
Answer: D. 0 Elements of a new int array are set to 0.
Which enhanced for loop prints every element of int[] arr?
- for (int x : arr) System.out.print(x);
- for (x : arr) System.out.print(x);
- for (int x = arr) System.out.print(x);
- 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.
Why is int max = 0; a poor way to start a search for the maximum?
- 0 isn't a valid starting value for an int variable in Java
- It causes a compile-time error, since max must start at null
- It only works for arrays of doubles, not arrays of ints
- 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.
Given int[] a = {4, 8, 15, 16, 23, 42};, what is a[a.length - 1]?
- 23
- 6
- An ArrayIndexOutOfBoundsException
- 42
Answer: D. 42 a.length is 6, so the last index is 5, which holds 42.
What happens? int[] arr = {1, 2, 3}; for (int x : arr) x = x * 2;
- Every element in the array is doubled in place
- The array is unchanged, because x is a copy of each element
- Only the first element of the array is doubled
- 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.
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; }
- Sorts the array
- Doubles each element
- Reverses the array in place
- Shifts every element left one position
Answer: C. Reverses the array in place It swaps pairs from both ends moving inward.
Accessing a[a.length] causes
- the last element to be returned
- the value 0 to be returned
- an ArrayIndexOutOfBoundsException
- a compile-time error
Answer: C. an ArrayIndexOutOfBoundsException Valid indices are 0 to a.length − 1.
More AP Computer Science A
- Unit 1: Primitive Types, Variables, and Expressions
- Unit 2: Using Objects: Strings, Math, and Wrappers
- Unit 3: Boolean Expressions and if Statements
- Unit 4: Iteration
- Unit 5: Writing Classes
- Unit 7: ArrayList
- Unit 8: 2D Arrays
- Unit 9: Recursion, Searching, and Sorting
Studying for a test in your own class? Make a practice test from your class notes.