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,741–2,750 of 2,811
  1. 2741
  2. 2742
  3. 2743
  4. 2744
  5. 2745
  6. 2746
  7. 2747
  8. 2748
  9. 2749
  10. 2750

Question 2741

Multiple choice Unit 4: Data Collections
Suppose the file 'words.txt' contains: hello world java code public static String readOddLines(String filename) { Scanner scan = new Scanner(new…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2742

Multiple choice Unit 4: Data Collections
public static int processAndReturn(ArrayList<Integer> nums) { int total = 0; for (int i = 0;i < nums.size();i++) { int val = nums.get(i);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 2743

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

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

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

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

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

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

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

Multiple choice Unit 4: Data Collections
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 2,741 of 2,811
Page 275 of 282