Skip to main content

Section 10.20 2018 AP Exam Free Response Questions

The following questions appeared on the 2018 AP Computer Science A exam. You can see all the free response questions from past exams at Past AP CSA Exams.

Subsection 10.20.1 2018 FRQ 1 - FrogSimulation A

Question 1. This question involves reasoning about a simulation of a frog hopping in a straight line. The frog attempts to hop to a goal within a specified number of hops. The simulation is encapsulated in the following FrogSimulation class. You will write two of the methods in this class.
public class FrogSimulation
{
    /** Distance, in inches, from the starting position to the goal. */
    private int goalDistance;

    /** Maximum number of hops allowed to reach the goal. */
    private int maxHops;
    
    /** Constructs a FrogSimulation where dist is the distance, in inches, from the starting
    * position to the goal, and numHops is the maximum number of hops allowed to reach the goal.
    * Precondition: dist >= 0; numHops >= 0
    */
    public FrogSimulation(int dist, int numHops)
    {
        goalDistance = dist;
        maxHops = numHops;
    }
    
    /** Returns an integer representing the distance, in inches, to be moved when the frog hops.
    */
    private int hopDistance()
    { /* implementation not shown */ }
    
    /** Simulates a frog attempting to reach the goal as described in part (a).
    * Returns true if the frog successfully reached or passed the goal during the simulation;
    * false otherwise.
    */
    public boolean simulate()
    { /* to be implemented in part (a) */ }
    
    /** Runs num simulations and returns the proportion of simulations in which the frog
    * successfully reached or passed the goal.
    * Precondition: num > 0
    */
    public double runSimulations(int num)
    { /* to be implemented in part (b) */ }
}
Part (a) Write the simulate method, which simulates the frog attempting to hop in a straight line to a goal from the frog’s starting position of 0 within a maximum number of hops. The method returns true if the frog successfully reached the goal within the maximum number of hops; otherwise, the method returns false.
The FrogSimulation class provides a method called hopDistance that returns an integer representing the distance (positive or negative) to be moved when the frog hops. A positive distance represents a move toward the goal. A negative distance represents a move away from the goal. The returned distance may vary from call to call. Each time the frog hops, its position is adjusted by the value returned by a call to the hopDistance method.
The frog hops until one of the following conditions becomes true:
  • The frog has reached or passed the goal.
  • The frog has reached a negative position.
  • The frog has taken the maximum number of hops without reaching the goal.
The following example shows a declaration of a FrogSimulation object for which the goal distance is 24 inches and the maximum number of hops is 5. The table shows some possible outcomes of calling the simulate method.
FrogSimulation sim = new FrogSimulation(24, 5);

Activity 10.20.1.

Complete method simulate below. You must use hopDistance appropriately to receive full credit.

Subsection 10.20.2 2018 FRQ 1 - FrogSimulation B

Part (b) Write the runSimulations method, which performs a given number of simulations and returns the proportion of simulations in which the frog successfully reached or passed the goal. For example, if the parameter passed to runSimulations is 400, and 100 of the 400 simulate method calls returned true, then the runSimulations method should return 0.25.

Activity 10.20.2.

Complete method runSimulations below. Assume that simulate works as specified, regardless of what you wrote in part (a). You must use simulate appropriately to receive full credit.

Subsection 10.20.3 2018 FRQ 2 - CodeWordChecker

Question 2. This question involves writing a CodeWordChecker class. (This question originally appeared as Question 3 and was written slightly differently. It has been updated to match the new content and test format.)
A CodeWordChecker object can be constructed with three parameters: two integers and a string. The first two parameters specify the minimum and maximum code word lengths, respectively, and the third parameter specifies a string that must not occur in the code word. A CodeWordChecker object can also be constructed with a single parameter that specifies a string that must not occur in the code word; in this case the minimum and maximum lengths will default to 6 and 20, respectively.
The following examples illustrate the behavior of CodeWordChecker objects.
Example 1
StringChecker sc1 = new CodeWordChecker(5, 8, "$");
Valid code words have 5 to 8 characters and must not include the string "$".
Example 2
StringChecker sc2 = new CodeWordChecker("pass");
Valid code words must not include the string "pass". Because the bounds are not specified, the length bounds are 6 and 20, inclusive.

Activity 10.20.3.

Write the complete CodeWordChecker class. Your implementation must meet all specifications and conform to all examples.

Subsection 10.20.4 2018 FRQ 3 - WordPair - A

Question 3. This question involves reasoning about pairs of words that are represented by the following WordPair class. (This question originally appeared as Question 2. It has been updated to match the new test format.)
public class WordPair
{
    /** Constructs a WordPair object. */
    public WordPair(String first, String second)
    { /* implementation not shown */ }

    /** Returns the first string of this WordPair object. */
    public String getFirst()
    { /* implementation not shown */ }
    
    /** Returns the second string of this WordPair object. */
    public String getSecond()
    { /* implementation not shown */ }
}
You will implement the constructor and another method for the following WordPairList class.
public class WordPairList
{
    /** The list of word pairs, initialized by the constructor. */
    private ArrayList<WordPair> allPairs;
    
    /** Constructs a WordPairList object as described in part (a).
    * Precondition: words.length >= 2
    */
    public WordPairList(String[] words)
    { /* to be implemented in part (a) */ }
    
    /** Returns the number of matches as described in part (b).
    */
    public int numMatches()
    { /* to be implemented in part (b) */ }
}
Part (a) Write the constructor for the WordPairList class. The constructor takes an array of strings words as a parameter and initializes the instance variable allPairs to an ArrayList of WordPair objects.
A WordPair object consists of a word from the array paired with a word that appears later in the array. The allPairs list contains WordPair objects (words[i], words[j]) for every i and j, where 0 <= i < j < words.length . Each WordPair object is added exactly once to the list.
The following examples illustrate two different WordPairList objects.
Example 1
String[] wordNums = {"one", "two", "three"}; WordPairList exampleOne = new WordPairList(wordNums);
After the code segment has executed, the allPairs instance variable of exampleOne will contain the following WordPair objects in some order.
("one", "two"), ("one", "three"), ("two", "three")
Example 2
String[] phrase = {"the", "more", "the", "merrier"}; WordPairList exampleTwo = new WordPairList(phrase);
After the code segment has executed, the allPairs instance variable of exampleTwo will contain the following WordPair objects in some order.
("the", "more"), ("the", "the"), ("the", "merrier"), ("more", "the"), ("more", "merrier"), ("the", "merrier")

Activity 10.20.4.

Complete the WordPairList constructor below.

Subsection 10.20.5 2018 FRQ 3 - WordPair - B

Part (b) Write the WordPairList method numMatches. This method returns the number of WordPair objects in allPairs for which the two strings match.
For example, the following code segment creates a WordPairList object.
String[] moreWords = {"the", "red", "fox", "the", "red"}; WordPairList exampleThree = new WordPairList(moreWords);
After the code segment has executed, the allPairs instance variable of exampleThree will contain the following WordPair objects in some order. The pairs in which the first string matches the second string are shaded for illustration.
("the", "red"), ("the", "fox"), ("the", "the"), ("the", "red"), ("red", "fox"), ("red", "the"), ("red", "red"), ("fox", "the"), ("fox", "red"), ("the", "red")
The call exampleThree.numMatches() should return 2.

Activity 10.20.5.

Complete method numMatches below.

Subsection 10.20.6 2018 FRQ 4 - ArrayTester - A

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}

Activity 10.20.6.

Complete the method getColumn below.

Subsection 10.20.7 2018 FRQ 4 - ArrayTester - B

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 10.20.1.
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 10.20.2.
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.

Activity 10.20.7.

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