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.

Questions 951–960 of 1,222
  1. 951
  2. 952
  3. 953
  4. 954
  5. 955
  6. 956
  7. 957
  8. 958
  9. 959
  10. 960

Question 951

Multiple choice Arraylists

What is printed?

ArrayList<String> words = new ArrayList<String>();
words.add("cat"); words.add("dog"); words.add("car");
words.add("dot"); words.add("cup");

ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < words.size(); i++) {
    String w = words.get(i);
    if (w.substring(0, 1).equals("c")) {
        result.add(w);
    }
}
for (int i = result.size() - 1; i >= 0; i--) {
    if (result.get(i).indexOf("a") != -1) {
        result.remove(i);
    }
}
System.out.println(result);
Pick an answer, then check it. The solution opens in a window.

Question 952

Multiple choice Recursion

Consider the following methods. What is printed?

public static int sumDigits(int n) {
    if (n < 10) { return n; }
    return n % 10 + sumDigits(n / 10);
}

public static int digitalRoot(int n) {
    int s = sumDigits(n);
    if (s < 10) { return s; }
    return digitalRoot(s);
}

System.out.println(digitalRoot(9999));
Pick an answer, then check it. The solution opens in a window.

Question 953

Multiple choice Arrays

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 954

Multiple choice Arraylists

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 955

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 956

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 957

Multiple choice Arrays
int[] arr = {4, 7, 2};…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 958

Multiple choice Arrays
int[] arr = new int[3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 959

Multiple choice Arrays
int[] arr = null;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 960

Multiple choice Arrays
int[] arr = new int[3];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 951 of 1,222
Page 96 of 123