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.

Subtopic: Arrays Clear filter

Questions 301–310 of 341
  1. 301
  2. 302
  3. 303
  4. 304
  5. 305
  6. 306
  7. 307
  8. 308
  9. 309
  10. 310

Question 301

Multiple choice Arrays
Consider the following code: public static int countDrops(int[] temperatures) { int drops = 0; for (int i = 1;i < temperatures.length;i++) { if (temperatures[i]…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 302

Multiple choice Arrays
Consider the following code: public static void rearrange(int[] arr) { int last = arr[arr.length - 1]; for (int i = arr.length - 1;i > 0;i--) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 303

Multiple choice Arrays
Consider the following code: public static void doubleAll(int[] numbers) { for (int val : numbers) { val = val * 2; } }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 304

Multiple choice Arrays
public static int findSecondMin(int[] data) { int min = Integer.MAX_VALUE; int secondMin = Integer.MAX_VALUE; for (int val : data) { if (val < min) { secondMin…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 305

Multiple choice Arrays
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 306

Multiple choice Arrays
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 307

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 308

Multiple choice Arrays
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 309

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 310

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 301 of 341
Page 31 of 35