Skip to main content
Logo image

Section 4.35 Free Response Question (FRQ) 3 - ItemInventory

The third FRQ on the AP CSA Exam as of 2026 will be on Data Analysis with ArrayList and worth 5 points. Students will be provided with a scenario and its associated class(es). Students will write one method of a given class based on provided specifications and examples. The method requires students to use, analyze,and manipulate data in an ArrayList structure. (Arrays will not be tested in an FRQ.)
Here are two common patterns for ArrayList traversal loops that you will need to use in this FRQ:
for (Type obj : list)
{
    if (obj ....)
        ...
}

for(int i=0; i < list.size(); i++)
{
   if (list.get(i) ....)
       ...
}
The following problem is a good example FRQ 3 from the 2025-2026 AP CSA Course and Exam Description (CED).

Subsection 4.35.1 FRQ ItemInventory Description

The ItemInfo class is used to store information about an item at a store. A partial declaration of the ItemInfo class is shown below.
public class ItemInfo
{
    /**
    * Returns the name of the item
    */
    public String getName()
    { /* implementation not shown */ }

    /**
    * Returns a value greater than 0.0 that represents the
    * cost of a single unit of the item, in dollars
    */
    public double getCost()
    { /* implementation not shown */ }
    
    /**
    * Returns true if the item is currently available and
    * returns false otherwise
    */
    public boolean isAvailable()
    { /* implementation not shown */ }
    
    /* There may be instance variables, constructors, and
    methods that are not shown. */
}
The ItemInventory class maintains an ArrayList named inventory that contains all items at the store. A partial declaration of the ItemInventory class is shown below. (Note that it contains an ArrayList of ItemInfo objects.)
public class ItemInventory
{
    /** The list of all items at the store */
    private ArrayList<ItemInfo> inventory;
    
    /**
    * Returns the average cost of the available items
    * whose cost is between lower and upper, inclusive
    * Precondition: lower <= upper
    * At least one available element of
    * inventory has a cost between
    * lower and upper, inclusive.
    * No elements of inventory are null.
    */
    public double averageWithinRange(double lower, double upper)
    { /* to be implemented */}

    /* There may be instance variables, constructors, and methods
    that are not shown. */
}
Write the ItemInventory method averageWithinRange. The method should return the average cost of the available items in inventory whose cost is between the parameters lower and upper, inclusive.
Suppose inventory contains the following seven ItemInfo objects.
Figure 4.35.1. ItemInventory with ArrayList of 7 ItemInfo objects
For the inventory shown, averageWithinRange(10.0, 50.0) should return 25.0, which is equal to the average cost of the available items within the specified range (a $20 action figure, a $45 frying pan, and a $10 coffee mug). Although the watch is within the specified range, it is not available.
Complete method averageWithinRange.
/**
 * Returns the average cost of the available items
 * whose cost is between lower and upper, inclusive
 * Precondition: lower <= upper
 * At least one available element of
 * inventory has a cost between
 * lower and upper, inclusive.
 * No elements of inventory are null.
*/
public double averageWithinRange(double lower, double upper)

Subsection 4.35.2 The Algorithm for averageWithinRange

The following exercises will help you to develop the algorithm for the method averageWithinRange.

Activity 4.35.1.

Which of the following loops is the most appropriate for iterating through inventory (an ArrayList of ItemInfo objects) to access each ItemInfo object when the index is not directly needed?
  • for (int i = 0; i < inventory.length; i++)
  • This loop is incorrect because ArrayList does not have a length field.
  • for (ItemInfo item : inventory)
  • Yes, the enhanced for-each loop is the most appropriate and concise way to iterate through an ArrayList when you only need to access each element and not its index.
  • while (inventory.hasNext())
  • This loop is incorrect because ArrayList does not have a hasNext() method.
  • for (int i = 0; i <= inventory.size(); i++)
  • This loop has an off-by-one error. It should use i < inventory.size() instead of i <= inventory.size(). The enhanced for-each loop is still a simpler choice than a for loop with an index.

Activity 4.35.2.

You are iterating through the inventory ArrayList using an enhanced for-each loop, and currentItem represents the ItemInfo object in the current iteration:
for (ItemInfo currentItem : inventory) 
{
    // ...
}
Inside this loop, how would you get the cost of the currentItem?
  • currentItem.cost
  • Accessing cost directly like this would only be possible if cost were a public instance variable. However, according to the ItemInfo class declaration, the cost is accessed via a method.
  • currentItem.getCost()
  • Yes, the ItemInfo class provides a public double getCost() method to return the cost of the item.
  • getCost(currentItem)
  • This syntax suggests a static method call, but getCost() is an instance method that must be called on an ItemInfo object.
  • currentItem.getCost
  • Methods in Java require parentheses after their name to indicate a method call, even if they take no arguments.

Activity 4.35.3.

You are iterating through the inventory ArrayList, and currentItem is the ItemInfo object in the current iteration. Which of the following condition correctly checks if currentItem is available AND its cost is between lower and upper, inclusive?
  • currentItem.isAvailable() && 
       currentItem.getCost() > lower && currentItem.getCost() < upper
    
  • This condition correctly checks if the item is available, but the cost range (> lower and < upper) is exclusive. The problem requires the range to be inclusive, meaning it should include lower and upper.
  • currentItem.isAvailable() || 
       (currentItem.getCost() >= lower && currentItem.getCost() <= upper)
    
  • This condition uses the logical OR operator (||), which means the code block would execute if the item is available OR if its cost is within range, but the problem requires both conditions to be true.
  • currentItem.isAvailable() && 
       currentItem.getCost() >= lower && currentItem.getCost() <= upper
    
  • Yes, this statement correctly uses the logical AND operator (&&) to combine the availability check with the cost range check. It also correctly uses >= and <= to ensure the range is inclusive of lower and upper.
  • currentItem.isAvailable() && 
      (currentItem.getCost() > lower || currentItem.getCost() < upper)
    
  • This condition uses the logical OR operator (||) for the cost range check, which is incorrect. The problem requires both conditions to be true.
The method averageWithinRange should iterate through the inventory list, checking each ItemInfo object to see if it is available and if its cost is within the specified range. If both conditions are met, the cost of the item should be added to a running total and counted. Remember that to compute the average, we need to add up the costs of all items that meet the criteria and then divide by the number of such items.

Subsection 4.35.3 Solve FRQ ItemInventory

Write the averageWithinRange method for the ItemInventory class.

Activity 4.35.4.

Write the averageWithinRange method for the ItemInventory class.

Subsection 4.35.4 Grading Rubric for FRQ 3

The following AP rubric is used to grade the averageWithinRange method in the ItemInventory class. Each item is worth 1 point, for a total of 5 points. Did you receive all of the points? In class, your teacher may have you grade each others’ code.
Figure 4.35.2. Rubric for averageWithinRange
You have attempted of activities on this page.