Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Multiple Choice Questions

Work through AP Computer Science A multiple-choice questions on Data Collections, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Subtopic: 2D Arrays Clear filter

Questions 121–130 of 145
  1. 121
  2. 122
  3. 123
  4. 124
  5. 125
  6. 126
  7. 127
  8. 128
  9. 129
  10. 130

Question 121

Multiple choice 2D Arrays

What is printed?

int[][] grid = {{3, 1, 4, 1},
               {5, 9, 2, 6},
               {5, 3, 5, 8}};
int maxCol = 0;
int maxSum = 0;
for (int c = 0; c < grid[0].length; c++) {
    int colSum = 0;
    for (int r = 0; r < grid.length; r++) {
        colSum += grid[r][c];
    }
    if (colSum > maxSum) {
        maxSum = colSum;
        maxCol = c;
    }
}
System.out.println(maxCol + " " + maxSum);
Pick an answer, then check it. The solution opens in a window.

Question 122

Multiple choice 2D Arrays

What is printed?

int[][] m = {{1, 2, 3, 4},
             {5, 6, 7, 8},
             {9, 10, 11, 12}};
int sum = 0;
for (int r = 0; r < m.length; r++) {
    for (int c = 0; c < m[0].length; c++) {
        if ((r + c) % 2 == 0) sum += m[r][c];
    }
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 123

Multiple choice 2D Arrays

What is printed?

int[][] grid = new int[4][4];
for (int r = 0; r < 4; r++) {
    for (int c = 0; c < 4; c++) {
        if (r <= c) grid[r][c] = 1;
    }
}
int sum = 0;
for (int r = 0; r < 4; r++)
    for (int c = 0; c < 4; c++)
        sum += grid[r][c];
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 124

Multiple choice 2D Arrays

What is printed?

int[][] grid = {{5, 2, 8, 1},
                {3, 7, 4, 6},
                {9, 1, 3, 2},
                {4, 5, 6, 8}};
int sum = 0;
for (int i = 0; i < grid.length; i++) {
    sum += grid[i][i];
    sum += grid[i][grid.length - 1 - i];
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 125

Multiple choice 2D Arrays

What is printed?

int[][] m = {{1, 2, 3},
             {4, 5, 6},
             {7, 8, 9}};
int temp = m[0][1];
for (int i = 0; i < m.length - 1; i++) {
    m[i][1] = m[i + 1][1];
}
m[m.length - 1][1] = temp;
System.out.println(m[0][1] + " " + m[1][1] + " " + m[2][1]);
Pick an answer, then check it. The solution opens in a window.

Question 126

Multiple choice 2D Arrays
int[][] arr = new int[2][3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 127

Multiple choice 2D Arrays
int[][] arr = new int[2][3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 128

Multiple choice 2D Arrays
public static int countWordNull(String[][] arr) { int count = 0; for (int r = 0;r < arr.length;r++) { for (int c = 0;c < arr[r].length;c++) { if…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 129

Multiple choice 2D Arrays
Consider the following code: public static int findMaxRowSum(int[][] grid) { int maxSum = 0; for (int row = 0; row < grid.length; row++) { int rowSum = 0; for…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 130

Multiple choice 2D Arrays
Consider the following code: public static String buildOrder(int[][] grid) { String result = ""; for (int col = 0;col < grid[0].length;col++) { for (int row =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 121 of 145
Page 13 of 15