Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections FRQ

Work through AP Computer Science A free-response 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 31–40 of 50
  1. 31
  2. 32
  3. 33
  4. 34
  5. 35
  6. 36
  7. 37
  8. 38
  9. 39
  10. 40

Question 31

Free response 2D Arrays
This question involves reasoning about a number puzzle that is represented as a two-dimensional array of integers. Each element of the array initially contains…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 32

Free response 2D Arrays
The High School Coin Collection Club needs new software to help organize its coin collections. Each coin in the collection is represented by an object of the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 33

Free response 2D Arrays
A grayscale image can be represented as a 2D array of integers, where each value is a pixel intensity from 0 (black) to 255 (white). Consider the following…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 34

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 35

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 36

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 37

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 38

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 39

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 40

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 31 of 50
Page 4 of 5