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,751–2,760 of 2,811
  1. 2751
  2. 2752
  3. 2753
  4. 2754
  5. 2755
  6. 2756
  7. 2757
  8. 2758
  9. 2759
  10. 2760

Question 2751

Multiple choice Unit 4: Data Collections
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 2752

Multiple choice Unit 4: Data Collections
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 2753

Multiple choice Unit 4: Data Collections
Consider the following code segment.
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(2);
list.add(3);
list.add(2);

for (int i = 0; i < list.size(); i++)
{
    if (list.get(i) == 2)
    {
        list.remove(i);
    }
}
System.out.println(list);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 2754

Multiple choice Unit 4: Data Collections
Which of the following operations is valid for a primitive array declared as int[] a but not for an ArrayList<Integer> list?
Pick an answer, then check it. The solution opens in a window.

Question 2755

Multiple choice Unit 4: Data Collections
Consider the following code segment, which performs a binary search.
int[] a = { 2, 5, 8, 11, 14, 17, 20, 23, 26, 29 };
int target = 20;
int low = 0;
int high = a.length - 1;
int steps = 0;
int foundIdx = -1;
while (low <= high && foundIdx == -1)
{
    int mid = (low + high) / 2;
    steps++;
    if (a[mid] == target)
    {
        foundIdx = mid;
    }
    else if (a[mid] < target)
    {
        low = mid + 1;
    }
    else
    {
        high = mid - 1;
    }
}
System.out.println(steps);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 2756

Multiple choice Unit 4: Data Collections
Consider the following recursive method.
public static int f(int n)
{
    if (n <= 1)
        return 1;
    if (n % 2 == 0)
        return f(n / 2);
    return 1 + f(n - 1);
}
What value is returned by the call f(12)?
Pick an answer, then check it. The solution opens in a window.

Question 2757

Multiple choice Unit 4: Data Collections
Consider the following code segment, which implements insertion sort on an int array.
int[] arr = { 7, 3, 9, 1, 5 };
for (int i = 1; i < arr.length; 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;
}
What are the contents of arr immediately after the outer loop has completed three iterations (that is, after the iterations for i = 1, i = 2, and i = 3)?
Pick an answer, then check it. The solution opens in a window.

Question 2758

Multiple choice Unit 4: Data Collections
Consider the following code segment.
int[][] m = {
    { 1, 2, 3 },
    { 4, 5, 6 }
};
int[][] r = new int[m[0].length][m.length];
for (int i = 0; i < m.length; i++)
{
    for (int j = 0; j < m[0].length; j++)
    {
        r[j][i] = m[i][j];
    }
}
What 2-D array is stored in r after the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 2759

Multiple choice Unit 4: Data Collections
Consider the following code segment.
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
list.add(3, "Y");
list.remove(0);
System.out.println(list);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 2760

Multiple choice Unit 4: Data Collections
Consider the following code segment. ArrayList<String> a = new ArrayList<String>(); a.add("x"); a.add("y"); ArrayList<String>…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2,751 of 2,811
Page 276 of 282