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 161–170 of 195
  1. 161
  2. 162
  3. 163
  4. 164
  5. 165
  6. 166
  7. 167
  8. 168
  9. 169
  10. 170

Question 161

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 162

Free response 2D Arrays

A smart building uses a grid of temperature sensors installed in the ceiling of a large warehouse floor. The sensor readings are stored in a 2D integer array where each element represents the temperature in degrees at that sensor's location. The facilities team needs analysis tools to monitor heating patterns and identify problem areas.

PART A

Write the method public int[] rowMaximums(int[][] temps) that returns a new 1D array where each element is the maximum temperature value found in the corresponding row of temps.

For example, if temps is {{72, 75, 68}, {80, 77, 82}, {69, 71, 70}}, the method returns {75, 82, 71}.

public int[] rowMaximums(int[][] temps) {

}

PART B

Write the method public boolean hasHotspot(int[][] temps, int threshold) that returns true if there exists any \(2\times2\) sub-grid (four adjacent cells forming a square) where all four values are strictly greater than threshold. The method returns false otherwise.

For example, if temps is {{70, 85, 90}, {60, 88, 92}, {75, 80, 78}} and threshold is 80, the method returns true because the \(2\times2\) sub-grid at rows 0–1 and columns 1–2 contains {85, 90, 88, 92}, all greater than 80.

public boolean hasHotspot(int[][] temps, int threshold) {

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

Question 163

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 164

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 165

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 166

Free response 2D Arrays
Consider a class MessageGrid that stores words in a two-dimensional array. The 2D array grid may contain null rows, and a non-null row may contain null…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 167

Free response 2D Arrays
You will write the NumberGrid class, which stores integers in a two-dimensional array. The declaration of the NumberGrid class is shown below. You may assume…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 168

Free response 2D Arrays
You will write the WordGrid class, which stores words in a two-dimensional array. The declaration of the WordGrid class is shown below. You may assume that grid…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 169

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 170

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 161 of 195
Page 17 of 20