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.

Questions 91–100 of 143
  1. 91
  2. 92
  3. 93
  4. 94
  5. 95
  6. 96
  7. 97
  8. 98
  9. 99
  10. 100

Question 91

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 92

Free response Arraylists

An online community platform stores user posts as strings in an ArrayList. The moderation team needs tools to clean up duplicate content and identify trending topics. You are writing two analysis methods for the MessageBoard class.

PART A

Write the method public ArrayList<String> removeDuplicates(ArrayList<String> posts) that returns a new ArrayList containing only the first occurrence of each unique string, in original order.

For example, if posts is ["hello", "world", "hello", "java", "world", "hello"], the method returns ["hello", "world", "java"].

public ArrayList<String> removeDuplicates(ArrayList<String> posts) {

PART B

Write the method public String mostFrequent(ArrayList<String> posts) that returns the string appearing most often. If there is a tie, return the one that appears first in the list.

For example, if posts is ["cat", "dog", "cat", "bird", "dog", "cat"], the method returns "cat" (appears 3 times).

public String mostFrequent(ArrayList<String> posts) {
The answer and the full solution open in a window.

Question 93

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 94

Free response Arraylists

A neighborhood book club meets monthly to discuss and rate books. Members rate each book on a scale of 1 to 10, and the club maintains two parallel ArrayLists: one for book titles and one for their average ratings. The BookClub class provides methods to help members select their next read.

PART A

Write the method public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer> ratings, int threshold) that returns a new ArrayList containing only the titles whose corresponding rating is greater than or equal to threshold, preserving the original order.

For example, if titles is ["Dune", "1984", "Cosmos", "Beloved"] and ratings is [9, 7, 8, 6] with threshold 8, the method returns ["Dune", "Cosmos"].

public ArrayList<String> getTopRated(ArrayList<String> titles, ArrayList<Integer>
    ratings, int threshold) {

PART B

Write the method public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) that removes and returns the title with the highest rating. If there is a tie, remove and return the one appearing earliest in the list. Both the titles and ratings lists should have the corresponding element removed.

For example, if titles is ["Dune", "1984", "Cosmos"] and ratings is [9, 7, 9], the method returns "Dune" (first with highest rating 9) and both lists are reduced to ["1984", "Cosmos"] and [7, 9].

public String recommend(ArrayList<String> titles, ArrayList<Integer> ratings) {
The answer and the full solution open in a window.

Question 95

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 96

Free response Arrays
This question involves the design of a class that maintains a collection of tasks. The following Task class is provided. public class Task { private String…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 97

Free response Arraylists
This question involves the design of a class that maintains a collection of books on a library shelf. The following Book class is provided. public class Book {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 98

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 99

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 100

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 91 of 143
Page 10 of 15