# Free Response Questions

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

## 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](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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.

```java
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.

![UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.](images/trioUML.png)

*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.

![Example objects](images/menuItemObjs.png)

*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).

### Try and Solve It

#### Exercise 1

Write the Trio class (near the end of the code) 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.

> Open the `01-frqTrio` folder and edit `Trio.java`. Press **Run** to try it; **Run Tests** checks it (7 tests).

## 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](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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.

```java
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.

![UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.](images/trioUML.png)

*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.

![Example objects](images/menuItemObjs.png)

*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).

### Grading Rubric

Below is the grading rubric for the Trio class problem.

![The grading rubric for the Trio class problem.](images/TrioGrading.png)

*The grading rubric for the Trio class problem.*

### Practice Grading

The following is the first sample student response.

![The first sample student response to the Trio class problem.](images/TrioStudentSol1.png)

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

> Check your understanding: Should the student earn 1 point for the correct declaration of the `Trio` class? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_1

> Check your understanding: Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_2

> Check your understanding: Should the student earn 1 point for declaring the constructor correctly? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_3

> Check your understanding: Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_4

> Check your understanding: Should the student earn 1 point for correctly declaring the methods in the `MenuItem` interface (`getName` and `getPrice`)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_5

> Check your understanding: Should the student earn 1 point for correctly constructing the string to return from `getName` and making it available to be returned? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_6

> Check your understanding: Should the student earn 1 point for returning a constructed string from `getName`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_7

> Check your understanding: Should the student earn 1 point for correctly calculating the price and making it available to be returned from `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_8

> Check your understanding: Should the student earn 1 point for returning the calculated price in `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_9

> Check your understanding: What should the total score be for this student response (out of 9 points)? Enter it as a number (like 3). — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio1_total

## 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](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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.

```java
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.

![UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.](images/trioUML.png)

*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.

![Example objects](images/menuItemObjs.png)

*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).

### Grading Rubric

Below is the grading rubric for the Trio class problem.

![The grading rubric for the Trio class problem.](images/TrioGrading.png)

*The grading rubric for the Trio class problem.*

### Practice Grading

The following is the second sample student response.

![The start of the second sample student response to the Trio class problem.](images/TrioStudentSol2a.png)

*The start of the second sample student response to the Trio class problem.*

![The end of the second sample student response to the Trio class problem.](images/TrioStudentSol2b.png)

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

> Check your understanding: Should the student earn 1 point for the correct declaration of the `Trio` class? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_1

> Check your understanding: Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_2

> Check your understanding: Should the student earn 1 point for declaring the constructor correctly? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_3

> Check your understanding: Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_4

> Check your understanding: Should the student earn 1 point for correctly declaring the methods in the `MenuItem` interface (getName and getPrice)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_5

> Check your understanding: Should the student earn 1 point for correctly constructing the string to return from `getName` and making it available to be returned? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_6

> Check your understanding: Should the student earn 1 point for returning a constructed string from `getName`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_7

> Check your understanding: Should the student earn 1 point for correctly calculating the price and making it available to be returned from `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_8

> Check your understanding: Should the student earn 1 point for returning the calculated price in `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_9

> Check your understanding: What should the total score be for this student response (out of 9 points)? Enter it as a number (like 3). — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio2_total

## 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](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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.

```java
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.

![UML class diagram showing MenuItem is implemented by Sandwich, Salad, Drink, and Trio.](images/trioUML.png)

*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.

![Example objects](images/menuItemObjs.png)

*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).

### Grading Rubric

Below is the grading rubric for the Trio class problem.

![The grading rubric for the Trio class problem.](images/TrioGrading.png)

*The grading rubric for the Trio class problem.*

### Practice Grading

The following is the third sample student response.

![The start of the second sample student response to the Trio class problem.](images/TrioStudentSol3.png)

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

> Check your understanding: Should the student earn 1 point for the correct declaration of the `Trio` class? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_1

> Check your understanding: Should the student earn 1 point for declaring the private instance variables (sandwich, salad, and drink or name and price)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_2

> Check your understanding: Should the student earn 1 point for declaring the constructor correctly? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_3

> Check your understanding: Should the student earn 1 point for correctly initializing the appropriate instance variables in the constructor? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_4

> Check your understanding: Should the student earn 1 point for correctly declaring the methods in the `MenuItem` interface (getName and getPrice)? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_5

> Check your understanding: Should the student earn 1 point for correctly constructing the string to return from `getName` and making it available to be returned? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_6

> Check your understanding: Should the student earn 1 point for returning a constructed string from `getName`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_7

> Check your understanding: Should the student earn 1 point for correctly calculating the price and making it available to be returned from `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_8

> Check your understanding: Should the student earn 1 point for returning the calculated price in `getPrice`? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_9

> Check your understanding: What should the total score be for this student response (out of 9 points)? Enter it as a number (like 3). — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/free-response-questions.html#qtrio3_total

## 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.

### Try and Solve It

#### Exercise 2

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

> Open the `02-NumberGroupB` folder and edit `Range.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).

## 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.

- private ArrayList\<NumberGroup> groupList;

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:

- new Range(5, 8)
- new Range(10, 12)
- new Range(1, 6)

The following table shows the results of several calls to `contains`.

![Number Group](images/NumberGroup.png)

### Try and Solve It

#### Exercise 3

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

> Open the `03-NumberGroupC` folder and edit `MultiGroups.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).

## 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](https://www.savemyexams.com/ap/computer-science/college-board/a/past-papers/).

**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`.

```java
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.

```java
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}`

### Try and Solve It

#### Exercise 4

Complete the method `getColumn`.

> Open the `04-getColumn` folder and edit `ArrayTester.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).

## 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.

![Latin Square Ex](images/LatinSquareEx.png)

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.

![Latin Square Ex2](images/LatinSquareEx2.png)

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.

### Try and Solve It

#### Exercise 5

Complete the method `isLatin`.

> Open the `05-isLatinTwo` folder and edit `ArrayTester.java`. Press **Run** to try it; **Run Tests** checks it (3 tests).
