Verityprep
Question Bank

AP® Computer Science A Practice Questions

Work through AP Computer Science A practice 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 3,031–3,040 of 3,140
  1. 3031
  2. 3032
  3. 3033
  4. 3034
  5. 3035
  6. 3036
  7. 3037
  8. 3038
  9. 3039
  10. 3040

Question 3031

Free response Unit 4: Data Collections
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 3032

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 3033

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 3034

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 3035

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 3036

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 3037

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 3038

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 3039

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 3040

Free response Unit 4: Data Collections

SurveyResults analyzes survey scores. scores is a nonnull ArrayList, possibly empty, with every value in [0,100]. The constructor keeps the supplied list reference.

public class SurveyResults {
 private java.util.ArrayList scores
 public SurveyResults(java.util.ArrayList initial){scores=initial;}
 // Implement countAbove and trim.
}
OperationResult
Initial scores[85,42,90,42,73,100,15]
countAbove(50)4
trim(40,90)[85,42,90,42,73]
then trim(50,80)[73]

(a) Implement countAbove(int threshold), returning the number of scores strictly greater than threshold.

(b) Implement trim(int minAccept,int maxAccept), removing scores outside the inclusive range [minAccept,maxAccept], while preserving the relative order of all remaining values. Assume 0 <= minAccept <= maxAccept <= 100. Handle empty lists, removal of every element, and consecutive removals.

The answer and the full solution open in a window.
Question 3,031 of 3,140
Page 304 of 314