AP CSA Unit-Level MCQ
Array Mutation
Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.
What array mutation covers
Changing an array means assigning into an indexed position, as in arr[i] = value. The enhanced for loop cannot do this, because its variable is a copy of the element rather than the storage location itself. Arrays are reference types, so passing one to a method or assigning it to another variable shares the same underlying storage: changes made through either name are visible through both.
Where students lose points
The enhanced-for assignment trap catches almost everyone once. Aliasing is the deeper misunderstanding: students expect int[] b = a; to copy, then are surprised when writing through b changes a. Shifting elements also goes wrong when the loop direction overwrites values that are still needed.
How it shows up on the AP exam
Unit 4 content with a strong pass-by-reference thread running through it. Methods that modify arrays in place are a standard FRQ task and a favorite MCQ subject.
For practice use only.
Array Mutation MCQ Practice
AP CSA array mutation practice: assigning into elements, why enhanced for loops cannot modify arrays, shifting values, and array reference aliasing.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Array Mutation: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1};
for (int i = 0; i < data.length; i++) {
data[i] = data[i] * 2;
}
System.out.print(data[0] + ":" + data[5]);All 15 array mutation questions
Work through the interactive quiz above first. This is the full array mutation question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Array Mutation: track the final printed value.
int[] data = {4, 9, 2, 7, 5, 1}; for (int i = 0; i < data.length; i++) { data[i] = data[i] * 2; } System.out.print(data[0] + ":" + data[5]);- 4:2
- 8:2
- 4:1
- 8:1
Show worked solution
Correct answer: 8:2
Assigning through the index does change the array, unlike assigning to the loop variable of an enhanced for loop.
2.Code Tracing 12 - Array Mutation: follow the variable updates.
int[] a = {6, 7, 4, 9, 7, 3}; int[] b = a; b[0] = 99; System.out.print(a[0] + ":" + b[0]);- 99:6
- 6:6
- 99:99
- 6:99
Show worked solution
Correct answer: 99:99
Assigning an array to another variable copies the reference, not the contents, so both names see the same change.
3.Code Tracing 28 - Array Mutation: trace the branch and loop path.
int[] data = {9, 7, 2, 8, 10, 6}; int temp = data[0]; data[0] = data[data.length - 1]; data[data.length - 1] = temp; System.out.print(data[0] + ":" + data[5]);- 6:6
- The code does not compile.
- 6:9
- 9:6
Show worked solution
Correct answer: 6:9
The temporary variable is what makes the swap work. Without it the first assignment would overwrite the value still needed.
4.Code Tracing 39 - Array Mutation: watch the index changes.
int[] data = {11, 8, 4, 10, 6, 8}; for (int i = 0; i < data.length - 1; i++) { data[i] = data[i + 1]; } System.out.print(data[0] + ":" + data[1]);- 11:8
- 4:8
- The code does not compile.
- 8:4
Show worked solution
Correct answer: 8:4
Shifting left front-to-back overwrites each slot before it is read again, so the values cascade forward.
5.Code Tracing 50 - Array Mutation: evaluate the state change step by step.
int[] data = {13, 9, 6, 8, 8, 10}; for (int i = 1; i < data.length; i++) { data[i] += data[i - 1]; } System.out.print(data[5]);- 54
- 10
- 41
- 22
Show worked solution
Correct answer: 54
Each pass reads the already-updated previous slot, so the last element ends up holding the running total of the whole array.
6.What is printed by the following code segment?
int[] a = {1, 2, 3, 4, 5}; for (int i = 0; i < a.length - 1; i++) { a[i] = a[i] + a[i + 1]; } System.out.print(a[0] + " " + a[1] + " " + a[3]);- 3 5 7
- 3 5 9
- 3 6 9
- 1 2 4
Show worked solution
Correct answer: 3 5 9
Each element absorbs its right neighbour before that neighbour is itself changed, so the original values are used.
7.What is printed by the following code segment?
int[] a = {1, 2, 3, 4, 5}; for (int i = a.length - 1; i > 0; i--) { a[i] = a[i] + a[i - 1]; } System.out.print(a[1] + " " + a[4]);- 3 5
- 2 9
- 3 9
- 1 5
Show worked solution
Correct answer: 3 9
Working backwards means each left neighbour is still original when it is read.
8.What is printed by the following code segment?
int[] a = {5, 3, 8, 1}; int t = a[0]; for (int i = 0; i < a.length - 1; i++) { a[i] = a[i + 1]; } a[a.length - 1] = t; System.out.print(a[0] + "" + a[1] + a[2] + a[3]);- 5381
- 1538
- 8135
- 3815
Show worked solution
Correct answer: 3815
Saving the first element, shifting left, and restoring it at the end rotates the array.
9.What is printed by the following code segment?
int[] a = {1, 2, 3, 4, 5, 6}; int w = 0; for (int i = 0; i < a.length; i++) { if (a[i] % 3 != 0) { a[w++] = a[i]; } } System.out.print(w + " " + a[0] + a[1] + a[2] + a[3]);- 4 1245
- 4 1234
- 6 1245
- 2 1245
Show worked solution
Correct answer: 4 1245
The kept values are packed toward the front and the write index records how many survived.
10.What is printed by the following code segment?
int[] a = {1, 2, 3}; int[] b = a.clone(); b[0] = 9; System.out.print(a[0] + " " + b[0]);- 9 9
- 1 9
- 1 1
- 9 1
Show worked solution
Correct answer: 1 9
A clone produces an independent array, unlike a plain reference assignment.
11.What is printed by the following code segment?
int[] a = {4, 2, 7, 1}; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; } } } System.out.print(a[0] + "" + a[3]);- 41
- 12
- 17
- 74
Show worked solution
Correct answer: 17
A full bubble sort leaves the array ordered, so the ends hold the smallest and largest values.
12.What is printed by the following code segment?
int[] a = {1, 2, 3, 4}; for (int v : a) { v *= 10; } for (int i = 0; i < a.length; i++) { a[i] *= 10; } System.out.print(a[0] + " " + a[3]);- 100 400
- 1 4
- 10 400
- 10 40
Show worked solution
Correct answer: 10 40
The enhanced loop changes only copies, so only the indexed loop has any effect.
13.What is printed by the following code segment?
int[] a = {2, 4, 6, 8}; a[a[0]] = a[a[1] % a.length]; System.out.print(a[2]);- 2
- 6
- 8
- 4
Show worked solution
Correct answer: 2
The target index is 2 and the source index is 0, so the first element is copied into the third position.
14.What is printed by the following code segment?
int[] a = {1, 2, 3, 4, 5}; int mid = a.length / 2; for (int i = 0; i < mid; i++) { int t = a[i]; a[i] = a[a.length - 1 - i]; a[a.length - 1 - i] = t; } System.out.print(a[0] + "" + a[2] + a[4]);- 135
- 531
- 555
- 513
Show worked solution
Correct answer: 531
Swapping over half the array reverses it, and the middle element stays where it is.
15.What is printed by the following code segment?
int[] a = {9, 9, 9}; for (int i = 0; i < a.length; i++) { a[i] -= i; a[i] /= 3; } System.out.print(a[0] + "" + a[1] + a[2]);- 333
- 321
- 322
- 332
Show worked solution
Correct answer: 322
The values become 9, 8, and 7 before the division, and integer division truncates 8 and 7 to 2.
