Skip to main content

Section 5.9 Free Response Questions

Section 5.9.1 Free Response - Trio A

The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a β€œtrio,” which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
Interfaces are no longer on the AP CSA exam, but you can just treat an interface like a superclass. Just substitute the word class instead of interface below and your TrioClass can extend MenuItem instead of implementing the interface.
public interface MenuItem
{
    /**
     * @return the name of the menu item
     */
    String getName();

    /**
     * @return the price of the menu item
     */
    double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
Figure 5.9.1. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
Figure 5.9.2. Example objects
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β€œ/” and followed by a space and then β€œTrio”. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).

Subsection 5.9.1.1 Try and Solve It

Activity 5.9.1.

Write the Trio class (near the end of the code below) that implements the MenuItem interface (which is like extending a class). Your implementation must include a constructor that takes three parameters representing a sandwich, salad, and drink. The main method has code to test the result.

Section 5.9.2 Trio Student Solution 1

The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a β€œtrio,” which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
    /**
     * @return the name of the menu item
     */
    String getName();

    /**
     * @return the price of the menu item
     */
    double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
Figure 5.9.3. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
Figure 5.9.4. Example objects
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β€œ/” and followed by a space and then β€œTrio”. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).

Subsection 5.9.2.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.5. The grading rubric for the Trio class problem.

Subsection 5.9.2.2 Practice Grading

The following is the first sample student response.
Figure 5.9.6. The first sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric

Activity 5.9.2.

Should the student earn 1 point for the correct declaration of the Trio class?
  • Yes
  • This declares the class correctly as public class Trio implements MenuItem
  • No
  • What do you think is wrong with the class declaration?

Activity 5.9.3.

Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • Yes
  • All instance variables are declared private (sand, sal, and dri) and are of the appropriate type (Sandwich, Salad, and Drink)
  • No
  • What do you think is wrong with the instance variables declaration?

Activity 5.9.4.

Should the student earn 1 point for declaring the constructor correctly?
  • Yes
  • This solution declares the constructor as public Trio(Sandwich a, Salad b, Drink c)
  • No
  • What do you think is wrong with the constructor declaration?

Activity 5.9.5.

Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • Yes
  • This solution initializes the private instance variables (sand, sal, and dri) correctly with the values from the parameters (a,b, and c).
  • No
  • What do you think is wrong with the initialization of the instance variables in the constructor?

Activity 5.9.6.

Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • Yes
  • This solution contains correct declarations for public String getName() and public double getPrice().
  • No
  • To implement an interface the class must have a getName and getPrice method as defined by the MenuItem interface.

Activity 5.9.7.

Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • Yes
  • This solution doesn’t include the "Trio" at the end of the name so it loses this point.
  • No
  • While the name is mostly correct, it is missing the word "Trio" at the end which means it loses this point.

Activity 5.9.8.

Should the student earn 1 point for returning a constructed string from getName?
  • Yes
  • This solution does return the constructed string.
  • No
  • Even though the string is not correct it was constructed and returned.

Activity 5.9.9.

Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • Yes
  • This solution does compute the price correctly.
  • No
  • There are only 3 possibilities for which is the cheapest item and this correctly deals with the 3 cases.

Activity 5.9.10.

Should the student earn 1 point for returning the calculated price in getPrice?
  • Yes
  • This solution does return the calculated price.
  • No
  • What do you think is wrong with the return statement?

Activity 5.9.11.

Section 5.9.3 Trio Student Solution 2

The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a β€œtrio,” which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
    /**
     * @return the name of the menu item
     */
    String getName();

    /**
     * @return the price of the menu item
     */
    double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
Figure 5.9.7. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
Figure 5.9.8. Example objects
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β€œ/” and followed by a space and then β€œTrio”. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).

Subsection 5.9.3.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.9. The grading rubric for the Trio class problem.

Subsection 5.9.3.2 Practice Grading

The following is the second sample student response.
Figure 5.9.10. The start of the second sample student response to the Trio class problem.
Figure 5.9.11. The end of the second sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric

Activity 5.9.12.

Should the student earn 1 point for the correct declaration of the Trio class?
  • Yes
  • This declares the class correctly as public class Trio implements MenuItem
  • No
  • What do you think is wrong with the class declaration?

Activity 5.9.13.

Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • Yes
  • Remember that all instance variables should be declared private so that the class controls access to the variables.
  • No
  • The student did not make the instance variables private, so the student does not get this point.

Activity 5.9.14.

Should the student earn 1 point for declaring the constructor correctly?
  • Yes
  • This solution declares the constructor as public Trio(Sandwich s, Salad sa, Drink d)
  • No
  • What do you think is wrong with the constructor declaration?

Activity 5.9.15.

Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • Yes
  • This solution initializes the instance variables (sandwich, salad, and drink) correctly with the values from the parameters (s, sa, and d).
  • No
  • What do you think is wrong with the initialization of the instance variables in the constructor?

Activity 5.9.16.

Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • Yes
  • This solution contains correct declarations for public String getName() and public double getPrice().
  • No
  • To implement an interface the class must have a getName and getPrice method as defined by the MenuItem interface.

Activity 5.9.17.

Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • Yes
  • Look at what getName is supposed to return.
  • No
  • This solution doesn’t include the "/" between the sandwich and salad and between the salad and the drink and is also missing the "Trio" at the end of the name, so it loses this point.

Activity 5.9.18.

Should the student earn 1 point for returning a constructed string from getName?
  • Yes
  • This solution does return the constructed string, even if the string is not completely correct.
  • No
  • Even though the string is not correct it was constructed and returned.

Activity 5.9.19.

Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • Yes
  • What if b is equal to c but both are greater than a?
  • No
  • This does not always compute the price correctly (when b is equal to c and they are both greater than a, it should return b+c, not a+b).

Activity 5.9.20.

Should the student earn 1 point for returning the calculated price in getPrice?
  • Yes
  • This solution does return the calculated price, even if that price is not always correct.
  • No
  • This point is earned if the student attempted to calculate the price and returned what was calculated.

Activity 5.9.21.

Section 5.9.4 Trio Student Solution 3

The following is a free response question from 2014. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. The menu at a lunch counter includes a variety of sandwiches, salads, and drinks. The menu also allows a customer to create a β€œtrio,” which consists of three menu items: a sandwich, a salad, and a drink. The price of the trio is the sum of the two highest-priced menu items in the trio; one item with the lowest price is free. Each menu item has a name and a price. The four types of menu items are represented by the four classes Sandwich, Salad, Drink, and Trio. All four classes implement the following MenuItem interface.
public interface MenuItem
{
    /**
     * @return the name of the menu item
     */
    String getName();

    /**
     * @return the price of the menu item
     */
    double getPrice();
}
The following diagram shows the relationship between the MenuItem interface and the Sandwich, Salad, Drink, and Trio classes.
Figure 5.9.12. UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.
For example, assume that the menu includes the following items. The objects listed under each heading are instances of the class indicated by the heading.
Figure 5.9.13. Example objects
The menu allows customers to create Trio menu items, each of which includes a sandwich, a salad, and a drink. The name of the Trio consists of the names of the sandwich, salad, and drink, in that order, each separated by β€œ/” and followed by a space and then β€œTrio”. The price of the Trio is the sum of the two highest-priced items in the Trio; one item with the lowest price is free. A trio consisting of a cheeseburger, spinach salad, and an orange soda would have the name "Cheeseburger/Spinach Salad/Orange Soda Trio" and a price of $4.00 (the two highest prices are $2.75 and $1.25). Similarly, a trio consisting of a club sandwich, coleslaw, and a cappuccino would have the name "Club Sandwich/Coleslaw/Cappuccino Trio" and a price of $6.25 (the two highest prices are $2.75 and $3.50).

Subsection 5.9.4.1 Grading Rubric

Below is the grading rubric for the Trio class problem.
Figure 5.9.14. The grading rubric for the Trio class problem.

Subsection 5.9.4.2 Practice Grading

The following is the third sample student response.
Figure 5.9.15. The start of the second sample student response to the Trio class problem.
Apply the grading rubric shown above as you answer the following questions.
Apply the Grading Rubric

Activity 5.9.22.

Should the student earn 1 point for the correct declaration of the Trio class?
  • Yes
  • This declares the class correctly as public class Trio implements MenuItem
  • No
  • What do you think is wrong with the class declaration?

Activity 5.9.23.

Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)?
  • Yes
  • Do you see any instance variables declared here?
  • No
  • The student did not declare any instance variables.

Activity 5.9.24.

Should the student earn 1 point for declaring the constructor correctly?
  • Yes
  • This solution declares the constructor as public Trio(Sandwich sandwich, Salad salad, Drink drink)
  • No
  • What do you think is wrong with the constructor declaration?

Activity 5.9.25.

Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor?
  • Yes
  • This solution doesn’t have any instance variables declared and doesn’t try to use the parameter values.
  • No
  • There is no attempt to set the instance variables (which haven’t been declared) to the parameter values.

Activity 5.9.26.

Should the student earn 1 point for correctly declaring the methods in the MenuItem interface (getName and getPrice)?
  • Yes
  • To implement an interface the class must have both a getName and getPrice method.
  • No
  • This class is missing both the interface methods.

Activity 5.9.27.

Should the student earn 1 point for correctly constructing the string to return from getName and making it available to be returned?
  • Yes
  • While the toString method exists and correctly creates the name string, it is not called by a getName method.
  • No
  • Since there is no getName method this point can not be awarded.

Activity 5.9.28.

Should the student earn 1 point for returning a constructed string from getName?
  • Yes
  • While the toString method exists and correctly creates and returns the name string, it is not called by a getName method.
  • No
  • Since there is no getName method this point can not be awarded.

Activity 5.9.29.

Should the student earn 1 point for correctly calculating the price and making it available to be returned from getPrice?
  • Yes
  • While there is a method that calculates the price correctly, it is the wrong method.
  • No
  • There is no getPrice method so the student can not earn this point.

Activity 5.9.30.

Should the student earn 1 point for returning the calculated price in getPrice?
  • Yes
  • While there is a method that calculates the price correctly and returns it, it is the wrong method.
  • No
  • There is no getPrice method so the student can not earn this point.

Activity 5.9.31.

Section 5.9.5 Retired FRQ - Inheritance - NumberGroup - Part B

Part b. A range represents a number group that contains all (and only) the integers between a minimum value and a maximum value, inclusive. Write the Range class, which is a NumberGroup. The Range class represents the group of int values that range from a given minimum value up through a given maximum value, inclusive. For example, the declaration NumberGroup range1 = new Range(-3, 2); represents the group of integer values -3, -2, -1, 0, 1, 2.
Write the complete Range class. Include all necessary instance variables and methods as well as a constructor that takes two int parameters. The first parameter represents the minimum value, and the second parameter represents the maximum value of the range. You may assume that the minimum is less than or equal to the maximum. Write the contains method which returns true or false if a given int argument is within the range set up by the constructor.

Subsection 5.9.5.1 Try and Solve It

Activity 5.9.32.

Complete the class Range below with instance variables, a constructor, and a contains method.

Section 5.9.6 Retired FRQ - Inheritance - NumberGroup - Part C

Part c. The MultipleGroups class represents a collection of NumberGroup objects and is a NumberGroup. The MultipleGroups class stores the number groups in the instance variable groupList (shown below), which is initialized in the constructor.
Write the MultipleGroups method contains. The method takes an integer and returns true if and only if the integer is contained in one or more of the number groups in groupList.
For example, suppose multiple1 has been declared as an instance of MultipleGroups and consists of the three ranges created by the calls:
The following table shows the results of several calls to contains.
Figure 5.9.16.

Subsection 5.9.6.1 Try and Solve It

Write the method contains below in the class MultiGroups. (Note that the original AP Question involved a NumberGroup interface which has been replaced with inheritance here).

Activity 5.9.33.

Write the method contains below in the class MultiGroups.

Section 5.9.7 Retired FRQ - Arrays - ArrayTester - Part A

The following is a free response question from 2018. It was question 4 on the exam. You can see all the free response questions from past exams at Past AP CSA Exams.
Question 4. This question involves reasoning about arrays of integers. You will write two static methods, both of which are in a class named ArrayTester.
public class ArrayTester
{
    /**
     * Returns an array containing the elements of column c of arr2D in the same
     * order as they appear in arr2D. Precondition: c is a valid column index in
     * arr2D. Postcondition: arr2D is unchanged.
     */
    public static int[] getColumn(int[][] arr2D, int c)
    {
        /* to be implemented in part (a) */
    }

    /**
     * Returns true if and only if every value in arr1 appears in arr2.
     * Precondition: arr1 and arr2 have the same length. Postcondition: arr1 and
     * arr2 are unchanged.
     */
    public static boolean hasAllValues(int[] arr1, int[] arr2)
    {
        /* implementation not shown */
    }

    /** Returns true if arr contains any duplicate values; false otherwise. */
    public static boolean containsDuplicates(int[] arr)
    {
        /* implementation not shown) */
    }

    /**
     * Returns true if square is a Latin square as described in part (b); false
     * otherwise. Precondition: square has an equal number of rows and columns.
     * Precondition: square has at least one row.
     */
    public static boolean isLatin(int[][] square)
    {
        /* to be implemented in part (b) */
    }
}
Part a. Write a static method getColumn, which returns a one-dimensional array containing the elements of a single column in a two-dimensional array. The elements in the returned array should be in the same order as they appear in the given column. The notation arr2D [r][c] represents the array at row r and column c.
The following code segment initializes an array and calls the getColumn method.
int [] [] arr2D = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 9, 5, 3 }};
int[] result = ArrayTester.getColumn (arr2D, 1);
When the code segment has completed execution, the variable result result will have the following contents. result: {1, 4, 7, 5}

Subsection 5.9.7.1 Try and Solve It

Activity 5.9.34.

Complete the method getColumn below.

Section 5.9.8 Retired FRQ - Arrays - ArrayTester - Part B

Arrays are no longer part of an assigned FRQ. However, they are good practice with reading and answering FRQs.
Part b. Write the static method isLatin, which returns true if a given two-dimensional square array is a Latin square, and otherwise, returns false.
A two-dimensional square array of integers is a Latin square if the following conditions are true. 1) The first row has no duplicate values. 2) All values in the first row of the square appear in each row of the square. 3) All values in the first row of the square appear in each column of the square.
Figure 5.9.17.
The ArrayTester class provides two helper methods: containsDuplicates and hasAllValues. The method containsDuplicates returns true if the given one-dimensional array arr contains any duplicate values and false otherwise. The method hasAllValues returns true if and only if every value in arr1 appears in arr2. You do not need to write the code for these methods.
Figure 5.9.18.
Complete method isLatin below. Assume that getColumn works as specified, regardless of what you wrote in part (a). You must use getColumn, hasAllValues, and containsDuplicates appropriately to receive full credit.

Subsection 5.9.8.1 Try and Solve It

Activity 5.9.35.

Complete the method isLatin below.
You have attempted of activities on this page.