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,051–3,060 of 3,140
  1. 3051
  2. 3052
  3. 3053
  4. 3054
  5. 3055
  6. 3056
  7. 3057
  8. 3058
  9. 3059
  10. 3060

Question 3051

Free response Unit 4: Data Collections

An image-processing routine applies a smoothing filter to a rectangular grid of integer pixel values. For each cell in the input grid, the smoothed grid contains the integer average (truncated toward zero, as with Java's int division) of that cell and its orthogonal neighbors — the cells directly above, below, to the left, and to the right, when they exist.

A corner cell therefore averages three values (itself and its two in-bounds orthogonal neighbors); an edge cell averages four; and an interior cell averages five. Diagonal neighbors are never included. The original grid must not be modified.

Write a static method gridSmooth that takes a rectangular 2-D array grid of non-negative integers (at least one row and one column; every row has the same length) and returns a new 2-D array of the same dimensions containing the smoothed values, as defined above.

Method Signature

public static int[][] gridSmooth(int[][] grid)

Example 1

Given the 3×4 grid on the left, the result is shown on the right. Each cell shows the full computation for reference.

Input gridReturned grid
{ { 4, 8, 12, 0 },
  { 2, 6, 3, 5 },
  { 10, 1, 7, 9 } }
{ { 4, 7, 5, 5 },
  { 5, 4, 6, 4 },
  { 4, 6, 5, 7 } }

For instance, the value at row 1, column 1 is \((6 + 8 + 2 + 3 + 1) / 5 = 20 / 5 = 4\) (itself, up, left, right, down — truncated). The value at row 0, column 0 is \((4 + 8 + 2) / 3 = 14 / 3 = 4\) (itself, right, down — truncated). The value at row 2, column 3 is \((9 + 5 + 7) / 3 = 21 / 3 = 7\).

Example 2

A 1×1 grid maps to itself: gridSmooth({{ 42 }}) returns {{ 42 }} (the sum is 42, divided by 1).

A 1×3 grid behaves as if every cell is on the top and bottom edge: gridSmooth({{ 3, 6, 9 }}) returns {{ 4, 6, 7 }}, since \((3+6)/2 = 4\), \((6+3+9)/3 = 6\), and \((9+6)/2 = 7\).

The answer and the full solution open in a window.

Question 3052

Multiple choice Unit 4: Data Collections
Consider the following code segment. ArrayList<String> a = new ArrayList<String>(); a.add("x"); a.add("y"); ArrayList<String>…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3053

Multiple choice Unit 4: Data Collections
Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(10); list.add(20); list.add(30);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3054

Multiple choice Unit 4: Data Collections
Consider the following code segment. int[] arr = {3, 7, 2, 9, 5, 1, 9, 4}; int best = arr[0]; int idx = 0; for (int i = 1; i < arr.length;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3055

Multiple choice Unit 4: Data Collections
Consider the following code segment. int[][] m = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int sum = 0; for (int r = 0; r < m.length; r++) { for (int c = 0; c <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3056

Multiple choice Unit 4: Data Collections
A method is intended to insert target into an ArrayList<Integer> called nums immediately after the first element that is strictly greater than target. If no…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3057

Multiple choice Unit 4: Data Collections
Consider the following method.
public static int f(int n)
{
    if (n <= 1)
    {
        return n;
    }
    return n + f(n - 2);
}
What is returned by the call f(7)?
Pick an answer, then check it. The solution opens in a window.

Question 3058

Multiple choice Unit 4: Data Collections
Consider the following code segment. int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int total = 0; for (int r = 0; r < grid.length; r++) { for (int c = 0;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3059

Free response Unit 4: Data Collections

This question involves analyzing daily high temperatures recorded over a season. The partial class TempLog below stores the temperatures in a double array.

public class TempLog
{
    /** highs[k] is the recorded high temperature (in degrees Fahrenheit)
     * for day k, where 0 <= k < highs.length. highs is never null
     * and always contains at least one element. */
    private double[] highs;

    // Constructor not shown.

    /** Returns the number of days whose high temperature was
     * strictly greater than threshold, as described in part (a). */
    public int daysHotterThan(double threshold)
    {
        /* to be implemented in part (a) */
    }

    /** Returns the length of the longest run of consecutive days
     * whose high temperature was strictly greater than threshold,
     * as described in part (b). */
    public int longestHotStreak(double threshold)
    {
        /* to be implemented in part (b) */
    }
}

Part (a)

Write the daysHotterThan method. The method returns the number of entries in highs that are strictly greater than threshold.

For example, if highs contains {72.5, 80.0, 68.1, 95.4, 80.0, 81.2}, then:

CallReturned value
daysHotterThan(80.0)2 (only 95.4 and 81.2 are strictly greater)
daysHotterThan(100.0)0
daysHotterThan(0.0)6

Complete method daysHotterThan below.

/** Returns the number of days whose high temperature was
 * strictly greater than threshold, as described in part (a). */
public int daysHotterThan(double threshold)

Part (b)

Write the longestHotStreak method. The method returns the length of the longest run of consecutive days whose high temperature is strictly greater than threshold. If no day satisfies the condition, the method returns 0.

For example, if highs contains {60.0, 85.0, 90.0, 72.0, 95.0, 95.0, 95.0, 70.0}, then:

CallReturned value
longestHotStreak(80.0)3 (days at indices 4, 5, 6)
longestHotStreak(88.0)3
longestHotStreak(100.0)0

In writing longestHotStreak, you may assume daysHotterThan works as specified regardless of what you wrote in part (a). Complete method longestHotStreak below.

/** Returns the length of the longest run of consecutive days
 * whose high temperature was strictly greater than threshold,
 * as described in part (b). */
public int longestHotStreak(double threshold)
The answer and the full solution open in a window.

Question 3060

Free response Unit 4: Data Collections
This question involves a movie streaming service that stores a list of movies and selects pairings for "double feature" slots — two movies that together fit…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3,051 of 3,140
Page 306 of 314