Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Practice Questions

Work through AP Computer Science A practice 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,111–1,120 of 1,222
  1. 1111
  2. 1112
  3. 1113
  4. 1114
  5. 1115
  6. 1116
  7. 1117
  8. 1118
  9. 1119
  10. 1120

Question 1111

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 1112

Free response Arraylists
A LibraryCart stores books that a student plans to check out. public class Book { / * * Returns the title of this book. * / public String getTitle() { / *…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1113

Free response 2D Arrays
A TerrainMap object stores elevation values in a rectangular 2D array. public class TerrainMap { private int[][] heights; / * * To be written. * / public int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1114

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 1115

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 1116

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 1117

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 1118

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 1119

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 1120

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 1,111 of 1,222
Page 112 of 123