Verityprep
Question Bank

AP® Computer Science A FRQ Practice: Free-Response Questions with Solutions

Work through AP Computer Science A free-response questions unit by unit, 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 271–280 of 329
  1. 271
  2. 272
  3. 273
  4. 274
  5. 275
  6. 276
  7. 277
  8. 278
  9. 279
  10. 280

Question 271

Free response Unit 4: Data Collections
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 272

Free response Unit 4: Data Collections
Consider the following incomplete class definition for a grade book: public class StudentGradeBook { private ArrayList<Integer> grades; // values 0-100 public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 273

Free response Unit 4: Data Collections
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 274

Free response Unit 4: Data Collections
A sensor produces a stream of numeric readings. Consider the following incomplete class: public class SensorLog { private ArrayList<Double> readings; public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 275

Free response Unit 4: Data Collections
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 276

Free response Unit 4: Data Collections
The Arctic Research Station Polaris-7 collects hourly temperature readings from its sensor array. Scientists store these readings in an ArrayList<Double> and…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 277

Free response Unit 4: Data Collections
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 278

Free response Unit 4: Data Collections

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 279

Free response Unit 4: Data Collections

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 280

Free response Unit 4: Data Collections

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 271 of 329
Page 28 of 33