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,121–1,130 of 1,222
  1. 1121
  2. 1122
  3. 1123
  4. 1124
  5. 1125
  6. 1126
  7. 1127
  8. 1128
  9. 1129
  10. 1130

Question 1121

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

Free response Arraylists

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 1123

Free response 2D Arrays

A grayscale image is represented as a rectangular 2D array of pixel values, where each entry is an integer in [0, 255]. The class ImageFilter provides operations on such images.

public class ImageFilter
{
    /** The pixel grid. pixels[r][c] is the pixel at row r, column c.
    * Each value is in [0, 255]. The grid has at least one row and
    * one column, and every row has the same length. */
    private int[][] pixels;
    /** Constructs an ImageFilter around the given grid. */
    public ImageFilter(int[][] p)
    {
        pixels = p;
    }
    /** Returns the maximum pixel value in the row range
    * [startRow, endRow] (inclusive on both ends). Part (a) */
    public int brightestIn(int startRow, int endRow)
    {
        /* part (a) */;
    }
    /** Applies an "invert then clip" filter to every pixel of pixels,
    * modifying pixels in place. Part (b) */
    public void invertAndClip(int floor)
    {
        /* part (b) */;
    }
    // Other methods are not shown.
}

Part A

Write the method brightestIn. It returns the largest pixel value found anywhere in rows startRow through endRow of pixels, inclusive on both ends.

For example, suppose pixels is the following \(4 \times 5\) grid.

{ { 40, 12, 7, 30, 50 },
{ 15, 80, 20, 10, 5 },
{ 33, 33, 33, 33, 33 },
{ 99, 0, 60, 5, 42 } }

Then brightestIn(0, 1) returns 80, brightestIn(2, 2) returns 33, and brightestIn(0, 3) returns 99.

Assume 0 <= startRow <= endRow < pixels.length.


Part B

Write the method invertAndClip. It modifies pixels in place by applying the following to every pixel p:

  1. Compute the inverted value 255 − p.
  2. If that inverted value is strictly less than floor, replace the pixel with floor (clip upward).
  3. Otherwise, store the inverted value.

For example, if floor is 50 and a pixel has value 210, then \(255 - 210 = 45\), which is below 50, so the pixel becomes 50. If a pixel has value 100, then \(255 - 100 = 155\), which is not below 50, so the pixel becomes 155.

Using the \(4 \times 5\) example from Part A and calling invertAndClip(50), the resulting grid is:

{ { 215, 243, 248, 225, 205 },
{ 240, 175, 235, 245, 250 },
{ 222, 222, 222, 222, 222 },
{ 156, 255, 195, 250, 213 } }

(Every inverted value was already at least 50.) If instead the row { 240, 250, 252, 200, 220 } were present, calling invertAndClip(50) would produce { 50, 50, 50, 55, 50 } — four pixels clip up to the floor. Note that a pixel whose inverted value equals floor is not clipped. Assume 0 <= floor <= 255.

The answer and the full solution open in a window.

Question 1124

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

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 1126

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 1127

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 1128

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 1129

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 1130

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 1,121 of 1,222
Page 113 of 123