2D Arrays in AP Computer Science A
What is the general structure for traversing a 2D array?
A single for loop.
Nested for loops.
A while loop.
An if statement.
Consider the 'Snaking Around' example. Which part of the code is responsible for alternating the direction of traversal in each row?
The outer for loop.
The inner for loop.
The if statement with modulo.
The System.out.print statement.
Which of the following algorithms, when implemented on a 2D array, would require modification of the array's elements and therefore cannot effectively use enhanced for loops for primitive types?
Finding the sum of all elements.
Finding the maximum element.
Doubling each element in the array.
Counting the number of even elements.
Given the 2D array arrayB
as defined in the notes, what will be printed by the following code snippet?
java
for (int i = 0; i < arrayB.length; i++) {
for (int j = 0; j < arrayB[0].length; j++) {
System.out.print(arrayB[i][j] + " ");
}
}
1 2 3 4 5 6 7 8 9 10 11 12
1 5 9 2 6 10 3 7 11 4 8 12
9 10 11 12 5 6 7 8 1 2 3 4
4 3 2 1 8 7 6 5 12 11 10 9
Which code segment correctly traverses through different columns in reverse order?
i < array.length
i < array[0].length
int i = array.length() - 1; i >= 0; i--
int i = array[0].length - 1; i >= 0; i--
Given the exampleOne
method in the notes (Snaking Around example), what output would be generated if arrayB
was a 1x4 array: {{1, 2, 3, 4}}
?
1 2 3 4
4 3 2 1
1 2 3 4 4 3 2 1
No output (empty string)
What is the primary reason why enhanced for loops are not suitable for modifying the original values of primitive types within a 2D array in Java?
Enhanced for loops are slower than regular for loops.
Enhanced for loops do not provide access to the index of the elements.
Java is pass-by-value for primitive types, so enhanced for loops operate on copies.
Enhanced for loops cannot be nested.

How are we doing?
Give us your feedback and let us know how we can improve
Given the 2D array arrayB
as defined in the notes, what will be the output of the following code?
java
for (int i = arrayB.length - 1; i >= 0; i--) {
for (int j = arrayB[0].length - 1; j >= 0; j--) {
System.out.print(arrayB[i][j] + " ");
}
}
1 2 3 4 5 6 7 8 9 10 11 12
12 11 10 9 8 7 6 5 4 3 2 1
9 10 11 12 5 6 7 8 1 2 3 4
4 3 2 1 8 7 6 5 12 11 10 9
Which of the following code snippets correctly implements column-major traversal for a 2D array arr
?
java
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
}
java
for (int i = 0; i < arr[0].length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
}
java
for (int i = 0; i < arr[0].length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[j][i] + " ");
}
}
java
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
}
Given a 2D array of Student
objects, where Student
has a setName
method, can enhanced for loops be used to change the names of the Student
objects in the array, and why?
No, because enhanced for loops cannot modify objects.
No, because setName
is a private method.
Yes, because the enhanced for loop provides a reference to the actual Student
objects.
Yes, but only if the Student
class is immutable.