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,861–2,870 of 3,140
  1. 2861
  2. 2862
  3. 2863
  4. 2864
  5. 2865
  6. 2866
  7. 2867
  8. 2868
  9. 2869
  10. 2870

Question 2861

Multiple choice Unit 4: Data Collections

What is the return value of f(5)?

public static int f(int n) {
    if (n <= 0) return 0;
    if (n == 1) return 1;
    return f(n - 1) + f(n - 2) + 1;
}
Pick an answer, then check it. The solution opens in a window.

Question 2862

Multiple choice Unit 4: Data Collections

What is the content of arr after executing the following?

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

Question 2863

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 2864

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 2865

Multiple choice Unit 4: Data Collections

What is printed?

int[][] grid = {{5, 2, 8, 1},
                {3, 7, 4, 6},
                {9, 1, 3, 2},
                {4, 5, 6, 8}};
int sum = 0;
for (int i = 0; i < grid.length; i++) {
    sum += grid[i][i];
    sum += grid[i][grid.length - 1 - i];
}
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 2866

Multiple choice Unit 4: Data Collections

What is printed?

int[] data = {3, 7, 2, 8, 5, 1, 9, 4, 6};
int maxDiff = 0;
for (int i = 0; i < data.length - 1; i++) {
    int diff = Math.abs(data[i] - data[i + 1]);
    if (diff > maxDiff) {
        maxDiff = diff;
    }
}
System.out.println(maxDiff);
Pick an answer, then check it. The solution opens in a window.

Question 2867

Multiple choice Unit 4: Data Collections

What is printed?

public static int mystery(int n) {
    if (n <= 1) { return n; }
    return mystery(n - 1) + mystery(n - 2);
}
System.out.println(mystery(7));
Pick an answer, then check it. The solution opens in a window.

Question 2868

Multiple choice Unit 4: Data Collections

What is printed?

int[][] m = {{1, 2, 3},
             {4, 5, 6},
             {7, 8, 9}};
int temp = m[0][1];
for (int i = 0; i < m.length - 1; i++) {
    m[i][1] = m[i + 1][1];
}
m[m.length - 1][1] = temp;
System.out.println(m[0][1] + " " + m[1][1] + " " + m[2][1]);
Pick an answer, then check it. The solution opens in a window.

Question 2869

Multiple choice Unit 4: Data Collections

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 2870

Multiple choice Unit 4: Data Collections

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 2,861 of 3,140
Page 287 of 314