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: Arraylists Clear filter

Questions 211–220 of 225
  1. 211
  2. 212
  3. 213
  4. 214
  5. 215
  6. 216
  7. 217
  8. 218
  9. 219
  10. 220

Question 211

Multiple choice Arraylists
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 212

Multiple choice Arraylists
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 213

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 214

Free response Arraylists

A retail system models each item for sale as a Product and uses a Store object to hold every product currently tracked by the store. The two classes are shown below; only the method totalValueOfLowStock in Store is left for you to complete. Everything else is already written.

public class Product
{
    private String name;
    private double price;
    private int quantity;

    public Product(String theName, double thePrice, int theQuantity)
    {
        name = theName;
        price = thePrice;
        quantity = theQuantity;
    }

    public String getName()      { return name; }
    public double getPrice()     { return price; }
    public int getQuantity()   { return quantity; }
}

public class Store
{
    /** Inventory of products currently tracked by this store. Never null. */
    private ArrayList<Product> inventory;

    public Store(ArrayList<Product> startingInventory)
    {
        inventory = startingInventory;
    }

    /** Returns the total dollar value of all products in inventory whose current
     *  quantity is strictly less than threshold. The dollar value of a single
     *  product is its price multiplied by its quantity. If no product has a
     *  quantity below threshold, the method returns 0.0.
     *  @param threshold the cutoff quantity; assumed to be non-negative
     *  @return the sum of price * quantity over all low-stock products
     */
    public double totalValueOfLowStock(int threshold) { /* to implement */ }
}

Write the method totalValueOfLowStock. The method must iterate over every product in inventory and accumulate the dollar value price * quantity for each product whose current quantity is strictly less than threshold.

Example

Suppose inventory holds three products:

{name:"widget", price:4.50, quantity:2}, {name:"gizmo", price:10.00, quantity:7}, {name:"bolt", price:0.25, quantity:3}.

A call to totalValueOfLowStock(5) selects the "widget" (quantity 2) and the "bolt" (quantity 3) because 7 is not less than 5. The returned value is \(4.50 \times 2 + 0.25 \times 3 = 9.75\).

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

Question 215

Multiple choice Arraylists
Consider the following class. public class Reading { private String sensor; private double value; public Reading(String s, double v) { sensor = s; value = v; }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 216

Free response Arraylists

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 217

Multiple choice Arraylists
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 218

Multiple choice Arraylists
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 219

Free response Arraylists

Information about applicants for a scholarship program is stored in Applicant objects.

public class Applicant
{
    private String name;
    private int score;
    private boolean complete;

    public Applicant(String n, int s, boolean c)
    {
        name = n;
        score = s;
        complete = c;
    }

    public String getName()
    {
        return name;
    }

    public int getScore()
    {
        return score;
    }

    public boolean isComplete()
    {
        return complete;
    }
}

The ScholarshipGroup class stores a list of applicants.

public class ScholarshipGroup
{
    private ArrayList<Applicant> applicants;

    public ScholarshipGroup(ArrayList<Applicant> list)
    {
        applicants = list;
    }

    public ArrayList<String> finalists(int minScore)
    { /* to be written */ }
}

Write the method finalists, which returns a new ArrayList<String> containing the names of all applicants who satisfy both of the following conditions.

  • The applicant's form is complete.
  • The applicant's score is at least minScore.

Among the applicants who satisfy those conditions, only applicants whose score is equal to the highest qualifying score should appear in the returned list. Names in the returned list must appear in the same order as the corresponding applicants appear in applicants. If no applicant qualifies, the method returns an empty list.

For example, if the qualifying applicants have scores 81, 94, 94, 88, the returned list should contain the names of the two applicants whose score is 94, in their original order.

The answer and the full solution open in a window.

Question 220

Multiple choice Arraylists
Consider the following code segment.
ArrayList<Integer> values = new ArrayList<Integer>();
values.add(2);
values.add(5);
values.add(1);
values.add(5);

for (int i = values.size() - 1; i >= 0; i--)
{
    if (values.get(i) == 5)
        values.remove(i);
}

System.out.println(values);
What is printed?
Pick an answer, then check it. The solution opens in a window.
Question 211 of 225
Page 22 of 23