AP CSA FRQ Practice
AP CSA FRQ Practice
AP CSA FRQ Practice
1. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
The method longestStreak is intended to determine the longest substring of consecutive identical characters
in the parameter str and print the result.
For example, the call longestStreak("CCAAAAATTT!") should print the result "A 5" because the longest
substring of consecutive identical characters is "AAAAA".
Write the code segment to be used in the body of the method below. The parameter str is a properly initialized
String variable. Your implementation should conform to the example above.
Points earned:
· has an outer loop that would iterate over every character in , even if a bounds error occurs in an inner loop, causing
an early termination.
+1 [Skill 3.C] Accesses all substrings of consecutive identical characters in without bounds error.
+1 [Skill 3.C] Updates length and character of longest consecutive identical characters appropriately.
Response earns this point, but incurs general penalty z below if it...
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
maxLen) Line 20: { Line 21: maxLen = cur - i; Line 22: maxChar = curChar; Line 23: } Line 24: } Line 25:
System.out.println(maxChar + " " + maxLen); Line 26: }">
0 1 2 3 4 5 6
Canonical Solution:
maxLen) Line 20: { Line 21: maxLen = cur - i; Line 22: maxChar = curChar; Line 23: } Line 24: } Line 25:
System.out.println(maxChar + " " + maxLen); Line 26: }">
2. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
This question involves a simulation of a two-player game. In the game, two simulated players each start out with an
equal number of coins. In each round, each player chooses to spend either 1, 2, or 3 coins. Coins are then awarded to
each player according to the following rules.
Same rule: If both players spend the same number of coins, player 2 is awarded 1 coin.
Off-by-one rule: If the players do not spend the same number of coins and the positive difference between the
number of coins spent by the two players is 1, player 2 is awarded 1 coin.
Off-by-two rule: If the players do not spend the same number of coins and the positive difference between the
number of coins spent by the two players is 2, player 1 is awarded 2 coins.
The following is an example of a game played with a starting value of 10 coins and a game length of 5 rounds.
The game ends when the specified number of rounds have been played or when a player’s coin count is less than 3 at
the end of a round.
The CoinGame class contains the following instance variables and methods. You will write code segments to
complete two methods in the CoinGame class.
int startingCoins; - an instance variable that represents the starting number of coins for each player
int maxRounds; - an instance variable that represents the maximum number of rounds played in the game
public int getPlayer1Move()- Returns the number of coins (1, 2, or 3) that player 1 will spend.
public int getPlayer2Move(int round)- Returns the number of coins (1, 2, or 3) that player 2 will spend. You
will write a code segment for the body of this method, as described in part (a).
public void playGame()- Plays a simulated game between two players. You will write a code segment for
the body of this method, as described in part (b).
In the simulation, player 2 will always play according to the same strategy. The number of coins player 2 spends is
based on what round it is, as described below.
(a) You will write the code segment for the body of the method getPlayer2Move, which returns the number of
coins that player 2 will spend in a given round of the game. In the first round of the game, the parameter round has the
value 1, in the second round of the game, it has the value 2, and so on. The method returns 1, 2, or 3 based on the
following rules.
Write the code segment to complete the method getPlayer2Move below by assigning the correct value to result
to be returned. The parameter round is a properly initialized int variable.
You will write the code segment for the body of the method playGame, which simulates a game between player 1 and
player 2, based on the rules and example shown at the beginning of the question. Both player 1 and player 2 start the
game with startingCoins coins. Computer player 1 spends 1, 2, or 3 coins based on the value returned by the
method getPlayer1Move(). Computer player 2 spends 1, 2, or 3 coins based on the value returned by the method
getPlayer2Move(int round).
The game ends when maxRounds rounds have been played or when a player’s coin count is less than 3 at the end of a
round.
At the end of the game, the winner is determined according to the following rules.
If both players have the same number of coins at the end of the game, the method prints "tie game".
If player 1 has more coins than player 2, the method prints "player 1 wins".
If player 2 has more coins than player 1, the method prints "player 2 wins".
(b) Assume that getPlayer2Move works as specified, regardless of what you wrote in part (a) . You must use
getPlayer1Move and getPlayer2Move appropriately to receive full credit.
Points earned:
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
0 1 2
-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General Penalties)
Canonical Solution:
Points earned:
+1 [Skill 3.C] Uses instance variables to set initial coins and number of rounds to play
Response earns this point, but incurs general penalty x below if it...
• uses local variables for the player coins and the number of rounds but does not declare them.
Note that general penalty x can only be incurred once, even if the error is made multiple times.
+1 [Skill 3.C] Loops appropriate number of times and ends when is reached or when either player's coin
count is less than at the end of a round
+1 [Skill 3.A] For each round of the game, calls and calls with the parameter
reflecting the round number
Response earns this point, but incurs general penalty x below if it...
• uses local variables for the player moves but does not declare them.
Note that general penalty x can only be incurred once, even if the error is made multiple times.
+1 [Skill 3.C] Compares coins spent to determine equality, off by one, or off by two
+1 [Skill 3.C] Updates each player's coin total based on results and rules of the game
General Penalties:
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
Canonical Solution:
= 3) && (coins2 >= 3)) Line 9: { Line 10: int p1Move = getPlayer1Move(); Line 11: int p2Move =
getPlayer2Move(round); Line 12 is blank. Line 13: coins1 -= p1Move; Line 14: coins2 -= p2Move; Line 15 is blank. Line
16: if (p1Move == p2Move) Line 17: { Line 18: coins2 += 1; Line 19: } Line 20: else if (Math.abs(p1Move - p2Move) ==
1) Line 21: { Line 22: coins2 += 1; Line 23: } Line 24: else Line 25: { Line 26: coins1 += 2; Line 27: } Line 28: round++;
Line 29: } Line 30 is blank. Line 31: if (coins1 > coins2) Line 32: { Line 33: System.out.println("player 1 wins"); Line 34:
} Line 35: else if (coins2 > coins1) Line 36: { Line 37: System.out.println("player 2 wins"); Line 38: } Line 39: else Line
40: { Line 41: System.out.println("tie game"); Line 42: } Line 43: }">
0 1 2 3 4 5 6 7
+1 Uses instance variables to set initial coins and number of rounds to play (Points earned)
+1 Loops appropriate number of times and ends when is reached or when either player's
coin count is less than at the end of a round (Points earned)
+1 For each round of the game, calls and calls with the
parameter reflecting the round number (Points earned)
+1 Compares coins spent to determine equality, off by one, or off by two (Points earned)
+1 Updates each player's coin total based on results and rules of the game (Points earned)
+1 Determines winner based on number of coins at end of game (Points earned)
+1 Prints winner or tie in specified format (Points earned)
-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)
(General Penalties)
-1 (x) Local variables used but none declared (General Penalties)
Canonical Solution:
= 3) && (coins2 >= 3)) Line 9: { Line 10: int p1Move = getPlayer1Move(); Line 11: int p2Move =
getPlayer2Move(round); Line 12 is blank. Line 13: coins1 -= p1Move; Line 14: coins2 -= p2Move; Line 15 is blank. Line
16: if (p1Move == p2Move) Line 17: { Line 18: coins2 += 1; Line 19: } Line 20: else if (Math.abs(p1Move - p2Move) ==
1) Line 21: { Line 22: coins2 += 1; Line 23: } Line 24: else Line 25: { Line 26: coins1 += 2; Line 27: } Line 28: round++;
Line 29: } Line 30 is blank. Line 31: if (coins1 > coins2) Line 32: { Line 33: System.out.println("player 1 wins"); Line 34:
} Line 35: else if (coins2 > coins1) Line 36: { Line 37: System.out.println("player 2 wins"); Line 38: } Line 39: else Line
40: { Line 41: System.out.println("tie game"); Line 42: } Line 43: }">