Verityprep
Question Bank

AP® Computer Science A Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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,581–2,590 of 2,811
  1. 2581
  2. 2582
  3. 2583
  4. 2584
  5. 2585
  6. 2586
  7. 2587
  8. 2588
  9. 2589
  10. 2590

Question 2581

Multiple choice Unit 4: Data Collections

After exactly 2 passes of this partial insertion sort, what is the array?

int[] arr = {8, 3, 5, 1, 4};
for (int i = 1; i < 3; i++) {
    int key = arr[i];
    int j = i - 1;
    while (j >= 0 && arr[j] > key) {
        arr[j + 1] = arr[j];
        j--;
    }
    arr[j + 1] = key;
}
Pick an answer, then check it. The solution opens in a window.

Question 2582

Multiple choice Unit 4: Data Collections

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 2583

Multiple choice Unit 4: Data Collections

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 2584

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 2585

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 2586

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 2587

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 2588

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 2589

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 2590

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 2,581 of 2,811
Page 259 of 282