Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 1 – Methods and Control Structures
​
Unit 2: Selection and Iteration
​
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 101/10

1. This question involves a trivia game simulation. The TriviaSimulation class tracks the results of a series of questions and calculates a score based on those results. You will write a constructor and a method in the TriviaSimulation class.

</>Java
public class TriviaSimulation
{
    /** The history of question results, formatted as a string of statuses separated by spaces */
    private String resultHistory; // To be initialized in part (a)

    /** The total score earned in the simulation */
    private int score; // To be initialized in part (a)

    /**
     * Simulates a game with a specified number of questions,
     * as described in part (a)
     * Precondition: numQuestions > 0
     */
    public TriviaSimulation(int numQuestions)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the result of the next question.
     * Returns "Pass" if answered correctly, "Fail" otherwise.
     */
    public String getQuestionResult()
    { /* implementation not shown */ }

    /**
     * Returns the point value associated with a result status.
     * Precondition: status is "Pass" or "Fail"
     */
    public int getPoints(String status)
    { /* implementation not shown */ }

    /**
     * Returns the number of times "Pass" appears in resultHistory,
     * as described in part (b)
     */
    public int countPasses()
    { /* to be implemented in part (b) */ }
}
a.

Write the TriviaSimulation constructor, which simulates a game with numQuestions questions. The constructor should initialize resultHistory to an empty string and score to 0. It should then simulate the game by repeatedly calling the helper method getQuestionResult to obtain the result of each question.

For each question:

  • The point value of the result (obtained by calling getPoints) should be added to score.
  • The result string followed by a single space should be appended to resultHistory.

The simulation ends when numQuestions have been asked OR when the player fails two questions in a row. If getQuestionResult returns "Fail" for two consecutive calls, the simulation stops immediately, even if fewer than numQuestions have been asked.

The getQuestionResult method returns either "Pass" or "Fail". The getPoints method returns the integer point value for a given status.

Example 1

Consider the following calls to getQuestionResult within a simulation where numQuestions is 5.
Call NumberReturn Value
1"Pass"
2"Fail"
3"Pass"
4"Pass"
5"Fail"
In this case, there are no consecutive fails. The constructor should call getQuestionResult 5 times. The resultHistory should be set to "Pass Fail Pass Pass Fail " and score should be the sum of points for these 5 results.

Example 2

Consider the following calls to getQuestionResult within a simulation where numQuestions is 5.
Call NumberReturn Value
1"Pass"
2"Fail"
3"Fail"
After the 3rd call, the simulation stops because "Fail" was returned twice consecutively. The constructor should not make any further calls. The resultHistory should be set to "Pass Fail Fail " and score should be the sum of points for these 3 results.
Complete the TriviaSimulation constructor. You must use getQuestionResult and getPoints appropriately in order to receive full credit.
/**
 * Simulates a game with a specified number of questions,
 * as described in part (a)
 * Precondition: numQuestions > 0
 */
public TriviaSimulation(int numQuestions)
b.

Write the countPasses method, which returns the number of times the string "Pass" appears in the instance variable resultHistory.

Example

Assume resultHistory contains the following string:
resultHistory ValueReturn Value
"Pass Fail Pass Pass Fail "3
"Fail Fail "0
In the first example, "Pass" appears 3 times. In the second example, "Pass" appears 0 times.
Complete the countPasses method.
/**
 * Returns the number of times "Pass" appears in resultHistory,
 * as described in part (b)
 */
public int countPasses()
Timed

00:00

Response auto-saves






Pep

You’ve used your included FRQ score

Unlimited scoring comes with a plan.

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 carefully before 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.

public class TriviaSimulation
{
    /** The history of question results, formatted as a string of statuses separated by spaces */
    private String resultHistory; // To be initialized in part (a)

    /** The total score earned in the simulation */
    private int score; // To be initialized in part (a)

    /**
     * Simulates a game with a specified number of questions,
     * as described in part (a)
     * Precondition: numQuestions > 0
     */
    public TriviaSimulation(int numQuestions)
    { /* to be implemented in part (a) */ }

    /**
     * Returns the result of the next question.
     * Returns "Pass" if answered correctly, "Fail" otherwise.
     */
    public String getQuestionResult()
    { /* implementation not shown */ }

    /**
     * Returns the point value associated with a result status.
     * Precondition: status is "Pass" or "Fail"
     */
    public int getPoints(String status)
    { /* implementation not shown */ }

    /**
     * Returns the number of times "Pass" appears in resultHistory,
     * as described in part (b)
     */
    public int countPasses()
    { /* to be implemented in part (b) */ }
}