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,041–3,050 of 3,140
  1. 3041
  2. 3042
  3. 3043
  4. 3044
  5. 3045
  6. 3046
  7. 3047
  8. 3048
  9. 3049
  10. 3050

Question 3041

Free response Unit 4: Data Collections

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 3042

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

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

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

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

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

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

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

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

Free response Unit 4: Data Collections

A linguist is studying word usage in a corpus. Given an ArrayList<String> of the words that appear in a document (in order, with possible repetitions), the linguist wants the list of most common words in that document.

Write a static method mostFrequent that, given such a list, returns a new ArrayList<String> containing every word whose number of occurrences in words equals the maximum number of occurrences attained by any word. In the returned list:

  • each qualifying word appears exactly once, even if it occurs many times in words;
  • the qualifying words appear in the order of their first occurrence in words.

Two words are considered the same only if they are equal as strings (case-sensitive). The parameter words must not be modified. If words is empty, return an empty list.

Method Signature

public static ArrayList<String> mostFrequent(ArrayList<String> words)

Examples

Input words Returned list Explanation
["to","be","or","not","to","be"] ["to","be"] Both appear twice; "to" occurs first, then "be".
["red","blue","red","green","blue","red"] ["red"] Max count is 3 ("red"); "blue" has count 2.
["a","b","c","d"] ["a","b","c","d"] All words tie at count 1; order is preserved.
["Cat","cat","Cat"] ["Cat"] Case-sensitive: "Cat" has count 2, "cat" has count 1.
[] [] Empty input → empty output.
The answer and the full solution open in a window.
Question 3,041 of 3,140
Page 305 of 314