AP CSA Practice Test: Arrays
AP CSA Practice Test: Arrays
Unit 4: Data Collections | 10 Questions | AP Computer Science A
int[] arr = {2, 4, 6, 8, 10};
System.out.println(arr[arr.length - 1]);
Explanation: arr.length is 5. arr[5 - 1] = arr[4] = 10. This is the standard way to access the last element of an array.
Common Mistake: Using arr[arr.length] which would cause ArrayIndexOutOfBoundsException.
int[] arr = new int[5]; arr[0] = 3; System.out.println(arr[0] + arr[1]);
Explanation: new int[5] initializes all elements to 0. arr[0] is set to 3, arr[1] is still 0. 3 + 0 = 3.
Common Mistake: Forgetting that int arrays default to 0, not null or undefined.
max after this code?int[] arr = {23, 45, 12, 67, 34};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
Explanation: Standard find-maximum algorithm. Starts at arr[0]=23, then compares each element. 45>23 (max=45), 12<45, 67>45 (max=67), 34<67. Final max = 67.
Common Mistake: Initializing max to 0 instead of arr[0]. That can give wrong results if all values are negative.
int[] arr = {1, 2, 3, 4, 5};
for (int val : arr) {
val = val * 2;
}
System.out.println(arr[2]);
Explanation: Enhanced for loop creates a COPY of each element. Modifying val does NOT modify the array. arr[2] remains 3.
Common Mistake: Thinking enhanced for loop modifies the array. It only works with copies for primitives.
{3, 1, 4, 1, 5}?public static int mystery(int[] arr) {
int count = 0;
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] < arr[i + 1])
count++;
}
return count;
}
Explanation: Counts how many times an element is less than the next: 3<1? No. 1<4? Yes. 4<1? No. 1<5? Yes. Count = 2.
Common Mistake: Not stopping at arr.length - 1 (which prevents ArrayIndexOutOfBoundsException).
String[] arr = new String[3]; System.out.println(arr[0]);
Explanation: Object arrays (including String[]) initialize to null, not empty string. Only primitive arrays have numeric defaults (int to 0, double to 0.0, boolean to false).
Common Mistake: Confusing null with empty string. String arrays default to null, not "".
int[] arr = {5, 3, 8, 1, 9};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
Explanation: This swaps the first element with the last, second with second-to-last, etc. It only goes to length / 2 to avoid re-swapping. This reverses the array in place.
Common Mistake: Not recognizing the swap pattern. temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; is the standard swap.
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
Explanation: Arrays are reference types. b = a copies the reference, not the array. Both a and b point to the same array. Changing b[0] also changes a[0].
Common Mistake: Thinking b = a creates a copy. For arrays, you need Arrays.copyOf() to make a true copy.
int[] arr = {10, 20, 30, 40};
int sum = 0;
for (int i = 0; i < arr.length; i += 2) {
sum += arr[i];
}
System.out.println(sum);
Explanation: The loop increments by 2: i=0 (sum+=10=10), i=2 (sum+=30=40). i=4 fails the condition. Sum = 10 + 30 = 40. It only sums elements at even indices.
Common Mistake: Thinking i += 2 sums every other value starting from 20. It starts at index 0.
ArrayIndexOutOfBoundsException?int[] arr = {5, 10, 15, 20};
System.out.println(arr[0]); // Line 1
System.out.println(arr[3]); // Line 2
System.out.println(arr[4]); // Line 3
System.out.println(arr[-1]); // Line 4
Explanation: Valid indices are 0 to 3 (length - 1). Line 3: index 4 is out of bounds. Line 4: index -1 is out of bounds. Both cause ArrayIndexOutOfBoundsException.
Common Mistake: Missing the negative index. Java does not support negative indexing like Python.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]