AP CSA Practice Test: Arrays

AP CSA Practice Test: Arrays

Unit 4: Data Collections | 10 Questions | AP Computer Science A

Question 1
What is the output?
int[] arr = {2, 4, 6, 8, 10};
System.out.println(arr[arr.length - 1]);
A10
B8
CArrayIndexOutOfBoundsException
D5

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.

Question 2
What is the output?
int[] arr = new int[5];
arr[0] = 3;
System.out.println(arr[0] + arr[1]);
A3
B0
CNullPointerException
DCompile error

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.

Question 3
What is the value of 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];
}
A23
B34
C67
D45

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.

Question 4
What is the output?
int[] arr = {1, 2, 3, 4, 5};
for (int val : arr) {
    val = val * 2;
}
System.out.println(arr[2]);
A6
B3
C0
DCompile error

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.

Question 5
What does this method return for {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;
}
A2
B3
C4
D5

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).

Question 6
What is the output?
String[] arr = new String[3];
System.out.println(arr[0]);
A""
Bnull
C0
DCompile error

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 "".

Question 7
What does this code do?
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;
}
ASorts the array
BReverses the array
CDoubles all elements
DRemoves duplicates

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.

Question 8
What is the output?
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 99;
System.out.println(a[0]);
A1
B99
CCompile error
D0

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.

Question 9
What is the output?
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);
A100
B40
C30
D60

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.

Question 10
Which line causes 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
ALine 2
BLine 3
CLine 4
DLines 3 and 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.

0% 0/10

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]