Verityprep
Question Bank

AP® Computer Science A Practice Questions

Work through AP Computer Science A practice 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 2,871–2,880 of 3,140
  1. 2871
  2. 2872
  3. 2873
  4. 2874
  5. 2875
  6. 2876
  7. 2877
  8. 2878
  9. 2879
  10. 2880

Question 2871

Multiple choice Unit 4: Data Collections

After one pass of this code, what is arr[arr.length - 1]?

int[] arr = {2, 5, 1, 8, 3};
for (int i = 0; i < arr.length - 1; i++) {
    if (arr[i] > arr[i + 1]) {
        int temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
    }
}
Pick an answer, then check it. The solution opens in a window.

Question 2872

Multiple choice Unit 4: Data Collections

What is printed?

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++) {
    list.add(i * i);
}
// list = [1, 4, 9, 16, 25, 36]
for (int i = list.size() - 1; i >= 1; i -= 2) {
    list.set(i, list.get(i) - list.get(i - 1));
}
System.out.println(list);
Pick an answer, then check it. The solution opens in a window.

Question 2873

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 2874

Free response Unit 4: Data Collections

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 2875

Multiple choice Unit 4: Data Collections
int[] arr = {4, 7, 2};…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2876

Multiple choice Unit 4: Data Collections
int[] arr = new int[3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2877

Multiple choice Unit 4: Data Collections
int[] arr = null;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2878

Multiple choice Unit 4: Data Collections
int[] arr = new int[3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2879

Multiple choice Unit 4: Data Collections
Integer[] arr = new Integer[3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2880

Multiple choice Unit 4: Data Collections
ArrayList<Integer> list = null;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2,871 of 3,140
Page 288 of 314