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,061–3,070 of 3,140
  1. 3061
  2. 3062
  3. 3063
  4. 3064
  5. 3065
  6. 3066
  7. 3067
  8. 3068
  9. 3069
  10. 3070

Question 3061

Free response Unit 4: Data Collections
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 3062

Multiple choice Unit 4: Data Collections
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 3063

Multiple choice Unit 4: Data Collections
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 3064

Multiple choice Unit 4: Data Collections
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 3065

Multiple choice Unit 4: Data Collections
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 3066

Multiple choice Unit 4: Data Collections
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 3067

Multiple choice Unit 4: Data Collections
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 3068

Multiple choice Unit 4: Data Collections
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 3069

Free response Unit 4: Data Collections
A hardware store tracks how many units of a particular item are in stock at the end of each day. The class StockLog stores these daily counts in the array…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 3070

Free response Unit 4: Data Collections

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 3,061 of 3,140
Page 307 of 314