Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Practice Questions

Work through AP Computer Science A practice 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 151–160 of 195
  1. 151
  2. 152
  3. 153
  4. 154
  5. 155
  6. 156
  7. 157
  8. 158
  9. 159
  10. 160

Question 151

Multiple choice 2D Arrays
A method receives a 2D array int[][] m where all rows have…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 152

Free response 2D Arrays
A board game uses a 2D integer array where each cell holds a point value. Consider: public class GameBoard { private int[][] board; // rows x cols public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 153

Multiple choice 2D Arrays
What is printed? int[][] m = {{2,7,6}, {9,5,1}, {4,3,8}}; int sum = 0; for (int i = 0;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 154

Multiple choice 2D Arrays
This code finds the maximum value in each column and sums them. What is printed? int[][] grid = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; int result = 0; for (int c…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 155

Free response 2D Arrays
The Meridian Concert Hall uses a digital seating management system. The venue is represented as a 2D array of boolean values where true indicates an occupied…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 156

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 157

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 158

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 159

Free response 2D Arrays

A geographic survey team uses a 2D integer array to represent terrain elevation data. Each element stores the elevation in meters at that grid location. The team needs tools to analyze the border of the survey region and to detect notable peaks in the interior.

PART A

Write the method public int borderSum(int[][] grid) that returns the sum of all elements on the border of the grid (first row, last row, first column, last column). Each corner element is counted exactly once.

For example, if grid = {{5, 3, 8, 1}, {2, 7, 4, 9}, {6, 1, 3, 5}}, the border elements are 5, 3, 8, 1, 9, 5, 3, 1, 6, 2 and the sum is 43 (interior elements 7 and 4 are excluded).

public int borderSum(int[][] grid) {

}

PART B

Write the method public int countPeaks(int[][] grid) that returns the number of interior cells (not on any edge) that are strictly greater than all four of their direct neighbors (up, down, left, right).

For example, if grid = {{1, 2, 1, 3}, {4, 8, 3, 1}, {1, 2, 9, 4}, {2, 1, 5, 2}}, the interior cells are at (1,1)=8, (1,2)=3, (2,1)=2, (2,2)=9. Cell (1,1)=8 is greater than all neighbors (2, 4, 3, 2) → peak. Cell (2,2)=9 is greater than all neighbors (3, 5, 2, 4) → peak. The method returns 2.

public int countPeaks(int[][] grid) {

}
The answer and the full solution open in a window.

Question 160

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 151 of 195
Page 16 of 20