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,081–3,090 of 3,140
  1. 3081
  2. 3082
  3. 3083
  4. 3084
  5. 3085
  6. 3086
  7. 3087
  8. 3088
  9. 3089
  10. 3090

Question 3081

Free response Unit 4: Data Collections

A cinema management system models each scheduled showing as a Movie object and keeps every showing currently on the schedule inside a Theater object. The two classes are shown below; only the method averageRatingAtLeast in Theater is left for you to complete. Everything else is already written.

public class Movie
{
    private String title;
    private int lengthMinutes;
    private double rating;

    public Movie(String theTitle, int theLength, double theRating)
    {
        title = theTitle;
        lengthMinutes = theLength;
        rating = theRating;
    }

    public String getTitle()        { return title; }
    public int getLengthMinutes()   { return lengthMinutes; }
    public double getRating()      { return rating; }
}

public class Theater
{
    /** All movies currently scheduled at this theater. Never null. */
    private ArrayList<Movie> showings;

    public Theater(ArrayList<Movie> startingShowings)
    {
        showings = startingShowings;
    }

    /** Returns the average rating of every movie in showings whose length is at least
     *  minLength minutes. If no movie meets the length requirement, returns 0.0.
     *  @param minLength the inclusive lower bound on length, in minutes; assumed non-negative
     *  @return the mean rating of qualifying movies, or 0.0 when no movie qualifies
     */
    public double averageRatingAtLeast(int minLength) { /* to implement */ }
}

Write the method averageRatingAtLeast. The method must iterate over every Movie in showings, accumulate the rating of each movie whose length in minutes is at least minLength, and return the mean of those ratings as a double. If no movie meets the length requirement, the method returns 0.0.

Example

Suppose showings holds four movies:

{title:"Alpha", lengthMinutes:90, rating:4.0}, {title:"Bravo", lengthMinutes:135, rating:4.5}, {title:"Charlie", lengthMinutes:75, rating:3.0}, {title:"Delta", lengthMinutes:120, rating:3.5}.

A call to averageRatingAtLeast(120) selects the movies whose length is at least 120 minutes — Bravo and Delta — and returns the mean of their ratings: (4.5 + 3.5) / 2 = 4.0.

A call to averageRatingAtLeast(200) finds no qualifying movie and therefore returns 0.0.

public double averageRatingAtLeast(int minLength)
The answer and the full solution open in a window.

Question 3082

Free response Unit 4: Data Collections
A classroom seating arrangement is stored in a class SeatingChart, which uses a two-dimensional String array to hold the name of the student assigned to each…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3083

Multiple choice Unit 4: Data Collections
Consider the following code segment. int[] a = {4, 7, 2, 9, 5}; int sum = 0; for…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3084

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);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3085

Multiple choice Unit 4: Data Collections
Consider the following array declaration. int[][] m = { {1, 2, 3}, {4, 5, 6},…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3086

Multiple choice Unit 4: Data Collections
Consider the following method. public static int f(int n) { if (n < 10)…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3087

Multiple choice Unit 4: Data Collections
Consider the following code segment, which uses binary search on a sorted array. int[] a = {2, 5, 8, 11, 14, 17, 20, 23, 26, 29}; int low = 0; int high =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3088

Multiple choice Unit 4: Data Collections
Consider the following method. public static int countMatches(int[] a, int[] b) { int count = 0; for (int i = 0; i < a.length; i++) { if (a[i]…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3089

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

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3090

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 <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3,081 of 3,140
Page 309 of 314