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 1,011–1,020 of 1,079
  1. 1011
  2. 1012
  3. 1013
  4. 1014
  5. 1015
  6. 1016
  7. 1017
  8. 1018
  9. 1019
  10. 1020

Question 1011

Multiple choice Arrays
public static int findSecondMin(int[] data) { int min = Integer.MAX_VALUE; int secondMin = Integer.MAX_VALUE; for (int val : data) { if (val < min) { secondMin…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1012

Multiple choice Arrays
Consider the following code segment, where arr is an int array.
int[] arr = {3, 1, 4, 1, 5, 9, 2, 6};
int count = 0;
for (int i = 0; i < arr.length - 1; i++)
{
    if (arr[i] < arr[i + 1])
    {
        count++;
    }
}
System.out.println(count);
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 1013

Multiple choice Arraylists
Consider the following method, intended to remove every String whose length is less than 3 from list.
public static void cleanup(ArrayList<String> list)
{
    for (int i = 0; i < list.size(); i++)
    {
        if (list.get(i).length() < 3)
        {
            list.remove(i);
        }
    }
}
Given the initial list ["a", "bb", "ccc", "dd", "eeee", "ff", "gg"], what does list contain after a call to cleanup(list)?
Pick an answer, then check it. The solution opens in a window.

Question 1014

Multiple choice 2D Arrays
Consider the following 2D array.
int[][] m =
{
    {1,  2,  3,  4},
    {5,  6,  7,  8},
    {9,  10, 11, 12},
    {13, 14, 15, 16}
};
What is printed by the following code segment?
int total = 0;
for (int r = 0; r < m.length; r++)
{
    total += m[r][m[r].length - 1 - r];
}
System.out.println(total);
Pick an answer, then check it. The solution opens in a window.

Question 1015

Multiple choice Recursion
Consider the following recursive method.
public static int mystery(int n)
{
    if (n <= 1)
    {
        return n;
    }
    return n + mystery(n / 2);
}
What value is returned by the call mystery(20)?
Pick an answer, then check it. The solution opens in a window.

Question 1016

Multiple choice Arrays
Consider the following code segment that processes an int array.
int[] arr = {2, 5, 1, 8, 3, 9, 4};
int best = arr[0] + arr[1];
for (int i = 1; i < arr.length - 1; i++)
{
    int s = arr[i] + arr[i + 1];
    if (s > best)
    {
        best = s;
    }
}
System.out.println(best);
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 1017

Multiple choice 2D Arrays
Consider the following code segment, which modifies a 2D array.
int[][] g =
{
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
for (int r = g.length - 1; r > 0; r--)
{
    g[r][0] = g[r - 1][0];
}
for (int r = 0; r < g.length; r++)
{
    System.out.print(g[r][0] + " ");
}
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 1018

Multiple choice Arraylists
Consider the following code segment.
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++)
{
    a.add(i * 3);
}
for (int i = 0; i < a.size(); i++)
{
    if (i % 2 == 0)
    {
        a.set(i, a.get(i) + i);
    }
}
System.out.println(a);
What is printed?
Pick an answer, then check it. The solution opens in a window.

Question 1019

Multiple choice Recursion
Consider the following recursive method.
public static int digitCount(int n)
{
    if (n < 10)
    {
        return 1;
    }
    return 1 + digitCount(n / 10);
}
What value is returned by the call digitCount(84263)?
Pick an answer, then check it. The solution opens in a window.

Question 1020

Multiple choice 2D Arrays
Consider the following code segment.
int[][] grid = {
    {  1,  2,  3,  4 },
    {  5,  6,  7,  8 },
    {  9, 10, 11, 12 }
};
int sum = 0;
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[0].length; c++)
    {
        if (r == c || r + c == grid[0].length - 1)
        {
            sum = sum + grid[r][c];
        }
    }
}
What value is stored in sum after the code segment executes?
Pick an answer, then check it. The solution opens in a window.
Question 1,011 of 1,079
Page 102 of 108