Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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 851–860 of 1,079
  1. 851
  2. 852
  3. 853
  4. 854
  5. 855
  6. 856
  7. 857
  8. 858
  9. 859
  10. 860

Question 851

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 852

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 853

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 854

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 855

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 856

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 857

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 858

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 859

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 860

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 851 of 1,079
Page 86 of 108