Mastering arrays is one of the most critical steps in scoring a 5 on the AP Computer Science A exam. Unit 6 introduces the fundamental concepts of how Java allocates memory for data structures. Before you can write complex sorting algorithms, you must understand exactly how arrays are created, initialized, and accessed.
In this interactive practice set, we cover Topic 6.1: Array Creation and Access. These 10 multiple-choice questions are designed to mirror the difficulty and trickery of the actual College Board exam.
Key Concepts to Remember Before You Start:
- Zero-Based Indexing: The first element of an array is always at index
0, and the last element is always atarray.length - 1. - Default Initialization: When you use the
newkeyword, Java automatically fills the array. Primitive numbers default to0, booleans default tofalse, and reference types (like Strings) default tonull. - Fixed Size: Once a Java array is created, its size can never be changed.
- The Out of Bounds Trap: Attempting to access an index that does not exist will immediately throw an
ArrayIndexOutOfBoundsException.
Test your knowledge with the interactive quiz below. Be sure to read the explanations for any questions you miss!
AP CSA 6.1: Array Creation & Access
numbers[0] = 4;
System.out.println(numbers[1]);
new keyword, Java automatically initializes all elements to their default value, which is 0. Because numbers[0] was set to 4, the element at numbers[1] remains at its default initialization of 0. Distractor C is incorrect because only reference types default to null.prices[4] = 10.50;
prices[4] goes past the end of the array, resulting in a runtime exception. Java arrays have fixed sizes and do not auto-expand.System.out.println(words[0].length());
String is a reference type. When an array of reference types is created using new, all elements default to null. Therefore, words[0] contains null. Attempting to call a method like .length() on a null reference throws a NullPointerException.int[] list2 = list1;
list2[0] = 99;
System.out.println(list1[0]);
list1 holds a memory address pointing to the array. The statement int[] list2 = list1; does not copy the array; it copies the memory address. Both variables point to the exact same array. Changing list2[0] changes list1[0].int lastElement = vals[vals.length];
length property returns the total number of elements, which is 10. Because arrays are zero-indexed, valid indices are 0 through 9. Accessing vals[10] causes an ArrayIndexOutOfBoundsException. The correct access is vals.length - 1.arr has been initialized with an odd number of integer elements. Which expression evaluates to the middle element?arr.length is 5, the valid indices are 0, 1, 2, 3, 4 (middle is 2). 5 / 2 evaluates to 2, so arr[arr.length / 2] correctly accesses index 2.boolean values is created using new, Java automatically initializes all elements to their default value, which is false.int x = data[1];
data[1] = data[2];
data[2] = x;
x stores the value at index 1 (10). Then, the value at index 2 (15) overwrites index 1. Finally, the value stored in x (10) overwrites index 2.arr.length returns the total number of elements in the array, not the highest valid index. Because arrays are zero-indexed, the highest valid index is always arr.length - 1.Unit 6.1 Practice Complete!
Why Array Memory References Matter
Many students struggle with the difference between primitive variables and reference variables. As you saw in the practice questions above, an array variable doesn’t actually hold the list of numbers—it holds a memory address pointing to where that list lives in the computer’s RAM.
If you set array2 = array1, you haven’t created a second array. You have simply created a second remote control pointing to the exact same TV. This is a favorite trick on the AP CSA exam, so make sure you understand it completely.
Ready for the Next Step?
Now that you understand how to create arrays and access individual elements, it’s time to learn how to check every single element in the list automatically using loops.
Continue your AP CSA review by moving on to Topic 6.2: Array Traversal Practice, where we break down standard for loops, while loops, and common off-by-one errors.