Mastering arrays is one of the most critical steps in scoring a 5 on the AP Computer Science A exam. Unit 4: Data Collections includes arrays as an important data abstraction. In Topic 4.3, you learn how one-dimensional arrays are created, initialized, and accessed using indexes.
In this interactive practice set, we cover Topic 4.3: Array Creation and Access. These 10 multiple-choice questions focus on array creation, initialization, length, indexing, and access.
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 4.3: 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 stores a reference to an array object. The statement int[] list2 = list1; does not create a second array; both variables refer to the same array object. Changing list2[0] therefore also 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 4.3 Practice Complete!
Understanding Array References
Many students struggle with the difference between primitive variables and reference variables. An array variable stores a reference to an array object. If two array variables refer to the same array object, changing an element through either variable changes that same array object.
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 4.4: Array Traversal Practice , where you will practice traversing arrays with loops and avoiding common off-by-one errors.