MC Review

Question 1: Which of the following declares and initializes a 2D array with 3 rows and 4 columns filled with zeros in Java?

A. int[][] array = new int[3][4];

B. int[3][4] array = new int[][];

C. int array[3][4] = new int[][];

D. int[][] array = new int[3][4];

Answer: A - Correctly initializes a 3x4 array with all elements set to zero by default.

Question 2: Given the following code snippet, what is printed?

int[][] array = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
System.out.println(array[1][2]);

A. 4

B. 5

C. 6

D. 9

Answer: C - The value at row 1 and column 2 is 6 (row and column indices start from 0).

Question 3: Which code snippet correctly calculates the sum of all elements in a 2D array “array”?

A.

int sum = 0;
for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[0].length; j++) {
        sum += array[i][j];
    }
}

B.

int sum = 0;
for (int[] row : array) {
    for (int value : row) {
        sum += value;
    }
}

C.

int sum = 0;
for (int i = 0; i < array[0].length; i++) {
    for (int j = 0; j < array.length; j++) {
        sum += array[j][i];
    }
}

D. Both A and B

Answer: D - Both A and B correctly sum up all elements in the 2D array.

Question 4: Which of the following best describes how to access the length of a specific row in a jagged 2D array?

A. array.length

B. array[0].length

C. array[rowIndex].length

D. array[rowIndex].getLength()

Answer: C - To find the number of elements in a specific row, use array[rowIndex].length

Question 5: What is the output of the following code?

int[][] array = {
    {3, 2, 1},
    {6, 5, 4},
    {9, 8, 7}
};
int total = 0;
for (int i = 0; i < array.length; i++) {
    total += array[i][i];
}
System.out.println(total);

A. 15

B. 9

C. 18

D. 12

Answer: A - The loop adds the diagonal elements: 3 (array[0][0]), 5 (array[1][1]), and 7 (array[2][2]). Total = 3 + 5 + 7 = 15.

AP CSA: 2D Arrays Cheat Sheet

Declaring/Initializing a 2D Array

Syntax:

int[][] array = new int[rows][columns];

Example (3 rows, 4 columns):

int[][] array = new int[3][4];

Direct Initialization:

int[][] array = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Accessing Elements

Use array[row][column] to access an element.

int value = array[1][2]; // Access element in row 1, column 2

Iterating Through a 2D Array

Using Nested Loops

Standard for loop:

for (int i = 0; i < array.length; i++) {          // Rows
    for (int j = 0; j < array[i].length; j++) {   // Columns
        System.out.println(array[i][j]);
    }
}

Enhanced for loop:

for (int[] row : array) {        // Each row is a 1D array
    for (int value : row) {      // Each element in the row
        System.out.println(value);
    }
}

Finding Dimensions

# of rows

int rows = array.length;

# of columns in a specific row

int columns = array[0].length;

Common Tasks

Calculating the Sum of All Elements

int sum = 0;
for (int[] row : array) {
    for (int value : row) {
        sum += value;
    }
}

Finding the Largest Element

int max = Integer.MIN_VALUE;
for (int[] row : array) {
    for (int value : row) {
        if (value > max) {
            max = value;
        }
    }
}

Printing a 2D Array row by row

for (int[] row : array) {
    System.out.println(Arrays.toString(row));
}

Jagged Arraays

A jagged array is a 2D array where rows can have different lengths.

int[][] jaggedArray = {
    {1, 2, 3},
    {4, 5},
    {6, 7, 8, 9}
};

Access row length dynamically

int rowLength = jaggedArray[rowIndex].length;