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,021–1,030 of 1,079
  1. 1021
  2. 1022
  3. 1023
  4. 1024
  5. 1025
  6. 1026
  7. 1027
  8. 1028
  9. 1029
  10. 1030

Question 1021

Multiple choice Arraylists
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 1022

Multiple choice Arraylists
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 1023

Multiple choice Sorting And Searching
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 1024

Multiple choice Recursion
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 1025

Multiple choice Arrays
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 1026

Multiple choice 2D Arrays
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 1027

Multiple choice Arraylists
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 1028

Multiple choice Arraylists
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 1029

Multiple choice Arraylists
Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1030

Multiple choice Arrays
Consider the following code segment. int[] arr = {3, 7, 2, 9, 5, 1, 9, 4}; int best = arr[0]; int idx = 0; for (int i = 1; i < arr.length;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,021 of 1,079
Page 103 of 108