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,141–1,150 of 1,222
  1. 1141
  2. 1142
  3. 1143
  4. 1144
  5. 1145
  6. 1146
  7. 1147
  8. 1148
  9. 1149
  10. 1150

Question 1141

Free response Arrays

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 1142

Free response Arraylists
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 1143

Free response 2D Arrays
A rectangular game grid of lights is represented by the partial class LightBoard below. Each entry of the 2D array is true if the light at that position is on…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1144

Multiple choice 2D Arrays
Consider the following code segment. int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int sum = 0; for (int r = 0; r < grid.length; r++) { for (int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1145

Multiple choice Recursion
Consider the following method. public static int mystery(int n) { if (n <= 0) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1146

Multiple choice Sorting And Searching
Consider using selection sort (which repeatedly finds the minimum of the unsorted portion and swaps it with the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1147

Multiple choice Sorting And Searching
Consider using insertion sort (processing index i from 1 upward, inserting arr[i] into the sorted prefix arr[0..i-1]) on the array {8, 2, 6, 1, 4}. What is the array after the first three iterations (i = 1, 2, 3) of the outer loop?
Pick an answer, then check it. The solution opens in a window.

Question 1148

Multiple choice Arrays
Consider the sorted array {2, 4, 7, 11, 15, 20, 26, 33, 41} (indices 0 through 8). A binary search is performed for the target value 20. The search uses the…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1149

Multiple choice Using Text Files
A text file "items.txt" contains the following lines, one per line. apple,3 banana,5 cherry,2 Assume the file is opened successfully and the java.util.Scanner…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1150

Multiple choice Using Text Files
A text file "scores.txt" contains the following two lines. 85 90 78 92 88 95 Assume the file is opened successfully and the java.util.Scanner and java.io.File…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 1,141 of 1,222
Page 115 of 123