AP CSA FRQ Practice

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

AP COMPUTER SCIENCE A Scoring Guide

Unit 4 Progress Check: FRQ

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.

public static void longestStreak(String str)

longestStreak FRQ (6 points)

Points earned:

+1 [Skill 3.C] Accesses every character in without bounds error

Response earns the point if it…

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

· has a single loop that iterates over every character in

+1 [Skill 3.C] Accesses all substrings of consecutive identical characters in without bounds error.

Response does not earn the point if it...

· is not nested inside the loop used to access every character in

+1 [Skill 3.C] Compares consecutive characters

+1 [Skill 3.A] Gets individual characters from using calls to

+1 [Skill 3.C] Updates length and character of longest consecutive identical characters appropriately.

+1 [Skill 3.C] Prints the correct longest streak of characters

Response earns this point, but incurs general penalty z below if it...

· returns a value from the method

AP Computer Science A Page 1 of 11


Scoring Guide

Unit 4 Progress Check: FRQ

General Penalties:

-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

-1 (x) Local variables used but none declared

-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter)

-1 (z) Void method or constructor that returns a value

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

Total number of points earned (minus penalties) is equal to 6.

+1 Accesses every character in without bounds error (Points earned)


+1 Accesses all substrings of consecutive identical characters in without bounds error.(Points
earned)

Page 2 of 11 AP Computer Science A


Scoring Guide

Unit 4 Progress Check: FRQ

+1 Compares consecutive characters (Points earned)


+1 Gets individual characters from using calls to (Points earned)
+1 Updates length and character of longest consecutive identical characters appropriately. (Points
earned)
+1 Prints the correct longest streak of characters (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)
-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter)(General Penalties)
-1 (z) Void method or constructor that returns a value(General Penalties

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: }">

AP Computer Science A Page 3 of 11


Scoring Guide

Unit 4 Progress Check: FRQ

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.

Page 4 of 11 AP Computer Science A


Scoring Guide

Unit 4 Progress Check: FRQ

Player 1 Coin Player 2 Coin


Player Player Player 1 Coin Player 2 Coin
Round Count at Count at
1 2 Outcome Count at End Count at End
Number Beginning of Beginning of
Spends Spends of Round of Round
Round Round
Off-by-
one,
10 - 2 10 - 1 + 1
1 10 10 2 1 player 2
= 8 = 10
gains 1
coin
Same,
player 2 8 - 2 10 - 2 + 1
2 8 10 2 2
gains 1 = 6 = 9
coin
Off-by-
two,
6 - 1 + 2 9 - 3
3 6 9 1 3 player 1
= 7 = 6
gains 2
coins
Same,
player 2 7 - 2 6 - 2 + 1
4 7 6 2 2
gains 1 = 5 = 5
coin
Off-by-
two,
5 - 3 + 2 5 - 1
5 5 5 3 1 player 1
= 4 = 4
gains 2
coins
End of
4 4 Tie game
Game

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.

AP Computer Science A Page 5 of 11


Scoring Guide

Unit 4 Progress Check: FRQ

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.

If round is divisible by 3, then return 3.


If round is not divisible by 3 but is divisible by 2, then return 2.
If round is not divisible by 3 and is not divisible by 2, then return 1.

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.

public int getPlayer2Move(int round)


{
int result;
//Your code segment will be inserted here.
return result;
}

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.

Page 6 of 11 AP Computer Science A


Scoring Guide

Unit 4 Progress Check: FRQ

Write the code segment to complete the method playGame below.

public void playGame()

Part A - getPlayer2Move method (2 points)

Points earned:

+1 [Skill 3.C] Checks for divisibility by 2 or 3

+1 [Skill 3.C] Correctly assigns 1, 2, or 3 to be returned

General Penalties:

-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

Canonical Solution:

0 1 2

Total number of points earned (minus penalties) is equal to 2.

+1 Checks for divisibility by 2 or 3 (Points earned)


+1 Correctly assigns 1, 2, or 3 to be returned (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)

AP Computer Science A Page 7 of 11


Scoring Guide

Unit 4 Progress Check: FRQ

-1 (y) Destruction of persistent data (e.g., changing value referenced by parameter) (General Penalties)

Canonical Solution:

Part B - playGame method (7 points)

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

+1 [Skill 3.C] Determines winner based on number of coins at end of game

+1 [Skill 3.C] Prints winner or tie in specified format

Page 8 of 11 AP Computer Science A


Scoring Guide

Unit 4 Progress Check: FRQ

General Penalties:

-1 (w) Extraneous code that causes side-effect (e.g., printing to output, incorrect precondition check)

-1 (x) Local variables used but none declared

Canonical Solution:

AP Computer Science A Page 9 of 11


Scoring Guide

Unit 4 Progress Check: FRQ

= 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

Total number of points earned (minus penalties) is equal to 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:

Page 10 of 11 AP Computer Science A


Scoring Guide

Unit 4 Progress Check: FRQ

= 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: }">

AP Computer Science A Page 11 of 11

You might also like