Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 1 – Methods and Control Structures
​
Unit 1: Using Objects and Methods
​
AP CS A FRQ Types & Units

Each FRQ type tests specific skills taught in particular units. Here's why certain units appear for each question type:

FRQFocusUnitsWhy
FRQ 1Methods and Control Structures1-2Tests method writing, loops, conditionals - foundational programming from Units 1-2
FRQ 2Class Design3Tests class design, constructors, instance variables - all Unit 3 OOP content
FRQ 3Data Analysis with ArrayList4Tests using, analyzing, and manipulating data in an ArrayList - Unit 4 content
FRQ 42D Array4Tests 2D array traversal and manipulation - Unit 4 content

This mapping reflects College Board's exam structure - each FRQ type tests specific skills that are taught in particular units.

Practice FRQ 1 of 191/19

1. This question involves the TrailHiker class, which tracks a hiker's journey through a series of landmarks. The TrailHiker class uses a helper method, getNextLandmark, to determine the path taken. You will write a constructor and a method in the TrailHiker class.

</>Java
public class TrailHiker
{
    /** 
     * The history of landmarks visited, formatted as a sequence 
     * separated by dashes (e.g., "Start-River-Mountain").
     */
    private String trailHistory;

    /** 
     * The total distance hiked so far.
     */
    private int totalDistance;

    /**
     * Simulates a hike starting at startLandmark and continuing for
     * numStops, as described in part (a)
     * Precondition: startLandmark is a valid landmark name.
     *               numStops > 0
     */
    public TrailHiker(String startLandmark, int numStops)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the next landmark to visit based on the current landmark.
     * Returns null if the trail ends.
     * Precondition: current is a valid landmark name.
     */
    public String getNextLandmark(String current)
    { /* implementation not shown */ }

    /**
     * Returns the distance in miles to hike to the specified landmark.
     * Precondition: landmark is a valid landmark name.
     */
    public int getDistance(String landmark)
    { /* implementation not shown */ }

    /**
     * Returns the number of times the specified landmark appears
     * in the trailHistory, as described in part (b)
     * Precondition: landmark is not null.
     */
    public int countLandmark(String landmark)
    { /* to be implemented in part (b) */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
a.

Write the TrailHiker constructor, which simulates a hike. The constructor should initialize the instance variable trailHistory to the startLandmark and totalDistance to 0. It should then simulate numStops movements. In each movement, the constructor must obtain the next landmark by calling the helper method getNextLandmark using the most recently added landmark. If a next landmark exists (is not null), it is appended to trailHistory preceded by a dash "-", and the distance to that landmark (obtained by calling getDistance) is added to totalDistance. If getNextLandmark returns null, the hike ends early, and no further stops are attempted.

The helper methods getNextLandmark and getDistance are provided. getNextLandmark returns the name of the next location or null if the trail ends. getDistance returns the integer distance to a specific location.

Example 1

Consider the following calls to getNextLandmark and getDistance. Assume startLandmark is "Trailhead".
Method CallReturn Value
getNextLandmark("Trailhead")"Stream"
getDistance("Stream")2
getNextLandmark("Stream")"Peak"
getDistance("Peak")5
getNextLandmark("Peak")"Camp"
getDistance("Camp")3
A call to new TrailHiker("Trailhead", 3) should set trailHistory to "Trailhead-Stream-Peak-Camp" and totalDistance to 10 (0 + 2 + 5 + 3).

Example 2

Consider the following calls where the trail ends early. Assume startLandmark is "Gate".
Method CallReturn Value
getNextLandmark("Gate")"Meadow"
getDistance("Meadow")4
getNextLandmark("Meadow")null
A call to new TrailHiker("Gate", 5) should set trailHistory to "Gate-Meadow" and totalDistance to 4. The loop terminates early because getNextLandmark returned null.
Complete the TrailHiker constructor.
/**
 * Simulates a hike starting at startLandmark and continuing for
 * numStops, as described in part (a)
 * Precondition: startLandmark is a valid landmark name.
 *               numStops > 0
 */
public TrailHiker(String startLandmark, int numStops)
b.

Write the countLandmark method, which returns the number of times the parameter landmark appears in the instance variable trailHistory. You may assume trailHistory contains valid landmark names separated by dashes.

Example 1

Assume trailHistory contains "Start-River-Wood-River-End".
Method CallReturn Value
countLandmark("River")2
countLandmark("Wood")1
countLandmark("Mountain")0
The method counts non-overlapping occurrences of the landmark name within the history string.
Complete the countLandmark method. You must use built-in String methods appropriately.
/**
 * Returns the number of times the specified landmark appears
 * in the trailHistory, as described in part (b)
 * Precondition: landmark is not null.
 */
public int countLandmark(String landmark)
Timed

00:00

Response auto-saves






Pep

essential ap study content awaits..

Features
Testimonials
Testimonials
start studying →
FRQ Directions
Free Response Question Practice

This practice environment simulates the AP AP Computer Science A Free Response Questions section. Here are some guidelines:

  • Read each question carefullybefore responding. Pay attention to command verbs like "identify," "explain," "analyze," or "evaluate."
  • Use the timer to practice time management. You can pause, restart, or hide the timer as needed.
  • Mark for Review if you want to come back to a question later.
  • Your responses are saved automatically as you type. You can also use the drawing tool for questions that require diagrams or graphs.
  • Use the toolbar for formatting options like bold, italic, subscript, and superscript.
  • Navigate between questions using the Previous and Next buttons at the bottom of the screen.

Tip: Answer all parts of each question. Partial credit is often available, so even if you are unsure, provide what you know.