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 941–950 of 1,222
  1. 941
  2. 942
  3. 943
  4. 944
  5. 945
  6. 946
  7. 947
  8. 948
  9. 949
  10. 950

Question 941

Multiple choice 2D Arrays

What is printed?

int[][] grid = new int[4][4];
for (int r = 0; r < 4; r++) {
    for (int c = 0; c < 4; c++) {
        if (r <= c) grid[r][c] = 1;
    }
}
int sum = 0;
for (int r = 0; r < 4; r++)
    for (int c = 0; c < 4; c++)
        sum += grid[r][c];
System.out.println(sum);
Pick an answer, then check it. The solution opens in a window.

Question 942

Multiple choice Arraylists

What is printed?

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) { list.add(i * i); }
for (int i = 0; i < list.size() - 1; i++) {
    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 943

Multiple choice Recursion

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 944

Multiple choice Arrays

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 945

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 946

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 947

Multiple choice 2D Arrays

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 948

Multiple choice Arrays

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 949

Multiple choice Recursion

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 950

Multiple choice 2D Arrays

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 941 of 1,222
Page 95 of 123