0% found this document useful (0 votes)
28 views93 pages

AP CSP BigIdea3 Assignment Key

The document is a scoring guide for an AP Computer Science Principles assignment, presenting various algorithmic scenarios and their correct solutions. It covers topics such as heuristics, algorithms for calculating distances, updating scores, and selecting random students, among others. Each question includes the correct answer and an explanation of why it is correct.

Uploaded by

seed2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views93 pages

AP CSP BigIdea3 Assignment Key

The document is a scoring guide for an AP Computer Science Principles assignment, presenting various algorithmic scenarios and their correct solutions. It covers topics such as heuristics, algorithms for calculating distances, updating scores, and selecting random students, among others. Each question includes the correct answer and an explanation of why it is correct.

Uploaded by

seed2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

AP COMPUTER SCIENCE PRINCIPLES Scoring Guide

AP-CSP-BigIdea3-Assignment

1. For which of the following situations would it be best to use a heuristic in order to find a solution that runs in a
reasonable amount of time?
(A) Appending a value to a list of elements, which requires no list elements be examined.
Finding the fastest route that visits every location among locations, which requires possible routes
(B)
be examined.
Performing a binary search for a score in a sorted list of scores, which requires that fewer than
(C)
scores be examined.
Performing a linear search for a name in an unsorted database of people, which requires that up to
(D)
entries be examined.

Answer B

Correct. As this algorithm has a factorial efficiency, it does not run in a reasonable amount of time. A
heuristic approach can be used to find an approximate solution than can run in a reasonable amount of
time.

2. A student is creating an algorithm to display the distance between the numbers num1 and num2 on a number
line. The following table shows the distance for several different values.

Value of num1 Value of num2 Distance Between num1 and num2


5 2 3
1 8 7
-3 4 7

Which of the following algorithms displays the correct distance for all possible values of num1 and num2 ?

AP Computer Science Principles Page 1 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Step 1:
(A) Add num1 and num2 and store the result in the variable sum.

Step 2: Take the absolute value of sum and display the result.

Step 1:
Subtract num1 from num2 and store the result in the variable diff.
(B)
Step 2:
Take the absolute value of diff and display the result.

Step 1:
Take the absolute value of num1 and store it in the variable absNum1.

(C) Step 2:
Take the absolute value of num2 and store it in the variable absNum2.

Step 3:
Add absNum1 and absNum2 and display the result.

Step 1:
Take the absolute value of num1 and store it in the variable absNum1.

(D) Step 2:
Take the absolute value of num2 and store it in the variable absNum2.

Step 3:
Subtract absNum1 from absNum2 and display the result.

Answer B

Correct. Subtracting num1 from num2 will give the difference between the two numbers. Taking the
absolute value of the difference will give the distance as a positive number.

3. A certain game keeps track of the maximum and minimum scores obtained so far. If num represents the most
recent score obtained, which of the following algorithms correctly updates the values of the maximum and the
minimum?

Page 2 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

If num is greater than the minimum, set the minimum equal to num. Otherwise, if num is greater
(A)
than the maximum, set the maximum equal to num.
If num is less than the minimum, set the minimum equal to num. Otherwise, if num is greater than
(B)
the maximum, set the maximum equal to num.
If num is less than the minimum, set the minimum equal to num. Otherwise, if num is less than the
(C)
maximum, set the maximum equal to num.
If num is greater than the minimum, set the minimum equal to num. Otherwise, if num is less than
(D)
the maximum, set the maximum equal to num.

Answer B

Correct. The minimum needs to be updated if the new number is less than the minimum. The maximum
needs to be updated if the new number is greater than the maximum.

4. The figure below shows four grids, each containing a robot represented as a triangle. The robot cannot move to a
black square or move beyond the edge of the grid.

Which of the following algorithms will allow the robot to make a single circuit around the rectangular region of
black squares, finishing in the exact location and direction that it started in each of the four grids?

AP Computer Science Principles Page 3 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Step 1:
Keep moving forward, one square at a time, until the square to the right of the robot is

black.
(A)
Step 2:
Turn right and move one square forward.

Step 3:
Repeat steps 1 and 2 three more times.

Step 1:
Keep moving forward, one square at a time, until the square to the right of the robot is

(B) no longer black.

Step 2:
Turn right and move one square forward.

Step 3: Repeat steps 1 and 2 three more times.

Step 1:
Move forward three squares.
(C)
Step 2:
Turn right and move one square forward.

Step 3: If the square to the right of the robot is black, repeat steps 1 and 2.

Step 1:
Move forward three squares.

(D) Step 2:
Turn right and move one square forward.

Step 3:
If the square to the right of the robot is not black, repeat steps 1 and 2.

Answer B

Correct. In all four grids, the robot starts along the side of a black region. Step 1 moves the robot just
past the end of the black region. Step 2 rotates the robot to the right and moves it forward, placing the
robot along the next side of the black region. Step 3 will repeat steps 1 and 2 to move the robot around

Page 4 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

the other 3 sides to complete a circuit around the black region.

5. A programmer is creating an algorithm that will be used to turn on the motor to open the gate in a parking garage.
The specifications for the algorithm are as follows.

• The gate should not open when the time is outside of business hours.
• The motor should not turn on unless the gate sensor is activated.
• The motor should not turn on if the gate is already open.

Which of the following algorithms can be used to open the gate under the appropriate conditions?
Check if the time is outside of business hours. If it is, check if the gate sensor is activated. If it is, check
(A)
if the gate is closed. If it is, turn on the motor.
Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if
(B)
the gate is open. If it is, turn on the motor.
Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is not, check
(C)
if the gate is open. If it is not, turn on the motor.
Check if the time is during business hours. If it is, check if the gate sensor is activated. If it is, check if
(D)
the gate is open. If it is not, turn on the motor.

Answer D

Correct. The algorithm continues past the first check if the time is during business hours, the algorithm
continues past the second check if the gate sensor is activated, and the algorithm continues past the third
check if the gate is not open. Performing the checks in this order will ensure that the gate is opened only
under the appropriate conditions.

6. Three different numbers need to be placed in order from least to greatest. For example, if the numbers are ordered 9,
16, 4, they should be reordered as 4, 9, 16. Which of the following algorithms can be used to place any three
numbers in the correct order?
If the first number is greater than the last number, swap them. Then, if the first number is greater than
(A)
the middle number, swap them.
If the first number is greater than the middle number, swap them. Then, if the middle number is greater
(B)
than the last number, swap them.
If the first number is greater than the middle number, swap them. Then, if the middle number is greater
(C)
than the last number, swap them. Then, if the first number is greater than the last number, swap them.
If the first number is greater than the middle number, swap them. Then, if the middle number is greater
(D) than the last number, swap them. Then, if the first number is greater than the middle number, swap
them.

AP Computer Science Principles Page 5 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. After comparing the first number and the middle number and potentially swapping them, and
then comparing the middle number and the last number and potentially swapping them, the largest
number will be in the third position. Next, after comparing the first number and the middle number and
potentially swapping them, the three numbers will be ordered from least to greatest.

7. Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ?

Select two answers.

Page 6 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Step 1:
Set x to 0.

Step 2:
Increment x by 1.
A
Step 3:
If x is not divisible by 3, then display x.

Step 4:
Repeat steps 2 and 3 until x is 20.

Step 1:
Set x to 0.
B
Step 2: If x is divisible by 3, then display x.
Step 3:
Increment x by 1.

Step 4: Repeat steps 2 and 3 until x is greater than 20.

Step 1:
Set x to 1.
C
Step 2: If x is divisible by 3, then do nothing; otherwise display x.
Step 3:
Increment x by 1.

Step 4: Repeat steps 2 and 3 until x is 20.

Step 1:
Set x to 1.
D
Step 2: If x is divisible by 3, then do nothing; otherwise display x.
Step 3:
Increment x by 1.

Step 4: Repeat steps 2 and 3 until x is greater than 20.

Answer A

Correct. This algorithm displays the numbers 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, and 20. After 20
is displayed, the algorithm terminates.

AP Computer Science Principles Page 7 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. This algorithm displays the numbers 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, and 20. After 20
is displayed, x is incremented to 21. The value 21 is not displayed since it is a multiple of 3 and the
algorithm terminates.

8. A teacher has a goal of displaying the names of 2 students selected at random from a group of 30 students in a
classroom. Any possible pair of students should be equally likely to be selected. Which of the following algorithms
can be used to accomplish the teacher’s goal?

Page 8 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Step 1:
Assign each student a unique integer from 1 to 30.

Step 2:
Generate a random integer n from 1 to 15.

(A) Step 3:
Select the student who is currently assigned integer n and display the student’s name.

Step 4:
Generate a new random integer n from 16 to 30.

Step 5:
Select the student who is currently assigned integer n and display the student’s name.

Step 1:
Assign each student a unique integer from 1 to 30.

Step 2:
Generate a random integer n from 1 to 30.

(B) Step 3:
Select the student who is currently assigned integer n and display the student’s name.

Step 4:
Generate a new random integer n from 1 to 30.

Step 5:
Select the student who is currently assigned integer n and display the student’s name.

Step 1:
Assign each student a unique integer from 1 to 30.

Step 2:
Generate a random odd integer n from 1 to 29.

(C) Step 3:
Select the student who is currently assigned integer n and display the student’s name.

Step 4:
Generate a new random even integer n from 2 to 30.

Step 5:
Select the student who is currently assigned integer n and display the student’s name.

(D) Step 1:
Assign each student a unique integer from 1 to 30.

AP Computer Science Principles Page 9 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Step 2:
Generate a random integer n from 1 to 30.

Step 3:
Select the student who is currently assigned integer n and display the student’s name.

Step 4:
The student who was selected in the previous step is assigned 0. All other students are

reassigned a unique integer from 1 to 29.

Step 5:
Generate a new random integer n from 1 to 29.

Step 6:
Select the student who is currently assigned integer n and display the student’s name.

Answer D

Correct. This algorithm selects 1 student from the group of 30 students, then selects another student from
the remaining 29 students. Any possible pair of students is equally likely to be selected.

9. The variable age is to be used to represent a person’s age, in years. Which of the following is the most
appropriate data type for age ?
(A) Boolean
(B) number
(C) string
(D) list

Answer B

Correct. A person’s age is typically represented numerically, so a numeric variable is most appropriate.

10. The variable isOpen is to be used to indicate whether or not a store is currently open. Which of the following is
the most appropriate data type for isOpen ?

Page 10 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) Boolean
(B) number
(C) string
(D) list

Answer A

Correct. The status of whether a store is open can be represented using only the values true and
false, so a Boolean variable is most appropriate.

11. A teacher is writing a code segment that will use variables to represent a student’s name and whether or not the
student is currently absent. Which of the following variables are most appropriate for the code segment?
(A) A string variable named s and a Boolean variable named a
(B) A string variable named s and a numeric variable named n

(C) A string variable named studentName and a Boolean variable named isAbsent

(D) A string variable named studentName and a numeric variable named numAbsences

Answer C

Correct. A student name is best represented using a string. The status of whether a student is absent can
be represented using only the values true and false, so a Boolean variable is most appropriate.
For both variables, using meaningful variable names helps with the readability of the code segment.

12. In a certain game, the integer variable bonus is assigned a value based on the value of the integer variable
score.

• If score is greater than 100, bonus is assigned a value that is 10 times score.
• If score is between 50 and 100 inclusive, bonus is assigned the value of score.
• If score is less than 50, bonus is assigned a value of 0.

Which of the following code segments assigns bonus correctly for all possible integer values of score ?

Select two answers.

AP Computer Science Principles Page 11 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

IF(score > 100)


{
bonus ← score * 10
}
ELSE
{
IF(score ≥ 50)
A {
bonus ← score
}
ELSE
{
bonus ← 0
}
}
IF(score ≥ 50)
{
IF(score > 100)
{
bonus ← score * 10
}
ELSE
B {
bonus ← 0
}
}
ELSE
{
bonus ← score
}
IF(score < 50)
{
bonus ← 0
}
ELSE
{
IF(score ≥ 50)
C {
bonus ← score
}
ELSE
{
bonus ← score * 10
}
}
IF(score < 50)
{
bonus ← 0
}
D ELSE
{
IF(score > 100)
{

Page 12 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

bonus ← score * 10
}
ELSE
{
bonus ← score
}
}

Answer A

Correct. This code segment sets bonus to score * 10 when score > 100, sets bonus to
score when 100 ≥ score ≥ 50, and sets bonus to 0 when score < 50.

Answer D

Correct. This code segment sets bonus to 0 when score < 50, sets bonus to score * 10
when score > 100, and sets bonus to score when 100 ≥ score ≥ 50.

13. The cost of a customer’s electricity bill is based on the number of units of electricity the customer uses.

• For the first 25 units of electricity, the cost is $5 per unit.


• For units of electricity after the first 25, the cost is $7 per unit.

Which of the following code segments correctly sets the value of the variable cost to the cost, in dollars, of
using numUnits units of electricity?

AP Computer Science Principles Page 13 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

(D)

Answer C

Correct. If the number of units of electricity used is 25 or less, the cost is 5 times the number of units.
Otherwise, the cost is 5 times the first 25 units plus 7 times the number of units above 25. For examples,
if a customer used 32 units of electricity, they should be charged $5 for the first 25 and $7 for the
additional 7 units (32 – 25 = 7 units), for a total charge of $174.

14. Which of the following is a benefit of using a list as a data abstraction in a program?

Page 14 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) Lists often allow their size to be easily updated to hold as many data values as needed.
(B) Lists convert all elements to strings so that they can be inspected character-by-character.
(C) Lists prevent duplicate data values from appearing in the list.
(D) Lists are used to store all input data so that there is a running record of all user input.

Answer A

Correct. Lists are data abstractions that allow multiple elements to be stored in a single collection. Many
programming languages allow programmers to append, insert, or remove data elements, allowing the size
of the list to grow or shrink as needed.

15. A programmer has a need to round many numeric values to the nearest integer. Which of the following best
explains the benefit of using a list as a data abstraction in this situation?
(A) Keeping the numeric values in a list makes it easier to round a number to the nearest integer.
Keeping the numeric values in a list makes it easier to apply the same computation to every data
(B)
element.
Keeping the numeric values in a list makes it easier to prevent a program from unintentionally changing
(C)
the value of a variable.
Keeping the numeric values in a list makes it easier to prevent a program from attempting to access an
(D)
index beyond the length of the list.

Answer B

Correct. Using a list as a data abstraction can result in a program that is easier to develop and maintain. It
is easier to apply the same algorithm to every element in a list than to apply the same algorithm to many
separate variables.

16. Which of the following is a benefit of using a simulation instead of an experiment to make a prediction?

Select two answers.


A simulation allows investigation of a phenomenon without the real-world limitations on time, safety,
A
or budget.

B A simulation can be used to model real-world events that are impractical for experiments.

C A simulation always produces the same output, so its results can be verified.

D A simulation produces results that are more accurate than experimental results.

AP Computer Science Principles Page 15 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer A

Correct. Simulations often mimic real-world events with the purpose of drawing inferences, allowing
investigation of a phenomenon without having to consider real-world limitations.

Answer B

Correct. Simulations are most useful when real-world events are impractical for experiments.

17. Which of the following are benefits of procedural abstraction?

Select two answers.


Procedural abstraction prevents programmers from accidentally using the intellectual property of other
A
programmers.
B Procedural abstraction eliminates the need for programmers to document their code.

C Procedural abstraction makes it easier for people to read computer programs.

Procedural abstraction provides an opportunity to give a name to a block of code that describes the
D
purpose of the code block.

Answer C

Correct. Procedural abstraction helps improve code readability.

Answer D

Correct. Procedures created to solve subproblems of a larger problem are called by name.

18. A sorted list of numbers contains 200 elements. Which of the following is closest to the maximum number of list
elements that will need to be examined when performing a binary search for a particular value in the list?
(A) 5
(B) 8
(C) 100
(D) 200

Page 16 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. The binary search algorithm starts at the middle of the sorted list and repeatedly eliminates half
the elements until the desired value is found or all the elements have been eliminated. For a list with 200
elements, the list will be cut in half a maximum of 7 times (with a total of 8 elements examined). The list
starts with 200 elements, then is reduced to 100 elements, then to 50 elements, then to 25 elements, then
to 12 elements, then to 6 elements, then to 3 elements, and then, finally, to 1 element.

19. A time stamp indicates the date and time that a measurement was taken. A data scientist has a list containing 10,000
time stamps, sorted in chronological order. Which of the following is closest to the maximum number of values that
will need to be examined when performing a binary search for a value in the list?
(A) 10
(B) 15
(C) 5,000
(D) 10,000

Answer B

Correct. The binary search algorithm starts at the middle of the list and repeatedly eliminates half the
elements until the desired value is found or all the elements have been eliminated. For a list with 10,000
elements, the list will be cut in half a maximum of 13 times, with the total of 14 elements needing to be
examined. The list will start with 10,000 elements, then will be reduced to 5,000 elements, then to 2,500
elements, then to 1,250 elements, then to 625 elements, then to 312 elements, then to 156 elements, then
to 78 elements, then to 39 elements, then to 19 elements, then to 9 elements, then to 4 elements, then to 2
elements, and then, finally, to 1 element. Of the given values, 15 is closest to 14.

20. The list listOne is a sorted list of numbers that contains 700 elements. The list listTwo is a sorted list of
numbers that contains 900 elements. Let x represent the maximum number of list elements that will need to be
examined when performing a binary search for a value in listOne, and let y represent the maximum number of
list elements that will need to be examined when performing a binary search for a value in listTwo. Which of
the following statements about x and y is true?
(A) The value of x is approximately equal to the value of y.
(B) The value of x is approximately 10 less than the value of y.
(C) The value of x is approximately 13 less than the value of y.
(D) The value of x is approximately 200 less than the value of y.

AP Computer Science Principles Page 17 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer A

Correct. The binary search algorithm starts at the middle of the list and repeatedly eliminates half of the
elements until the desired value is found or all elements have been eliminated. For listOne, the list
will be cut in half a maximum of 9 times, with a total of 10 elements needing to be examined. The list
will start with 700 elements, then will be reduced to 350 elements, then to 175 elements, then to 87
elements, then to 43 elements, then to 21 elements, then to 10 elements, then to 5 elements, then to 2
elements, and then, finally, to 1 element. For listTwo, the list will also be cut in half a maximum of
9 times, with a total of 10 elements needing to be examined. The list will start with 900 elements, then
will be reduced to 450 elements, then to 225 elements, then to 112 elements, then to 56 elements, then to
28 elements, then to 14 elements, then to 7 elements, then to 3 elements, and then, finally, to 1 element.

Page 18 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

21. Consider a game where a player spins a wheel divided into four identical sections, labeled A, B, C, and D. If the
player spins A, the score is 10. If the player spins B, the score is 5. If the player spins C or D, the score is -1.

The following code segment is intended to implement the game.

AP Computer Science Principles Page 19 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Which of the following could be used as a replacement for <MISSING STATEMENT> so the code segment
works as intended?

Page 20 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

(D)

Answer B

Correct. The code segment sets score to 10 25% of the time, to 5 25% of the time, and to -1
the remaining 50% of the time.

22. Assume that both lists and strings are indexed starting with index 1.

The list wordList has the following contents.

["abc", "def\", "ghi", "jkl"]

Let myWord be the element at index 3 of wordList. Let myChar be the character at index 2 of
myWord. What is the value of myChar ?
(A) "e"
(B) "f\"

(C) "h"

(D) "i"

Answer C

Correct. The element at index 3 in wordList is "ghi". The character at index 2 of "ghi" is
"h".

AP Computer Science Principles Page 21 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

23. In the following code segment, assume that x and y have been assigned integer values.

sum 0
REPEAT x TIMES
{
REPEAT y TIMES
{
sum ← sum + 1
}
}

At the end of which of the following code segments is the value of sum the same as the value of sum at the end
of the preceding code segment?

Select two answers.


sum 0
z x + y
REPEAT z TIMES
A {
sum ← sum + 1
}
sum 0
z x * y
REPEAT z TIMES
B {
sum ← sum + 1
}
sum 0
REPEAT x TIMES
{
sum ← sum + 1
C }
REPEAT y TIMES
{
sum ← sum + 1
}
sum 0
REPEAT y TIMES
{
REPEAT x TIMES
D {
sum ← sum + 1
}
}

Page 22 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. Since the REPEAT loops are nested, the value of sum at the end of the original code segment
is x * y. The value of sum at the end of this code segment is also x * y.

Answer D

Correct. Since the REPEAT loops are nested, the value of sum at the end of the original code segment
is x * y. The value of sum at the end of this code segment is also x * y since the order of the
REPEAT loops does not matter.

24. Consider the following code segment, where exam and presentation are integer variables and grade is
a string variable.

IF((exam > 90) AND (presentation > 80))


{
grade ← "A"
}
IF((exam > 80) OR (presentation > 75))
{
grade ← "B"
}
ELSE
{
IF((exam > 70) OR (presentation > 60))
{
grade ← "C"
}
ELSE
{
IF(exam > 60)
{
grade ← "D"
}
ELSE
{
grade ← "F"
}
}
}

Under which of the following conditions will the value "C" be assigned to the variable grade ?

AP Computer Science Principles Page 23 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) When the value of exam is 70 and the value of presentation is 50


(B) When the value of exam is 70 and the value of presentation is 80

(C) When the value of exam is 80 and the value of presentation is 60

(D) When the value of exam is 80 and the value of presentation is 80

Answer C

Correct. When the code segment is executed, the condition ((exam > 90) AND (presentation
> 80)) in the first IF statement will be false. The condition ((exam > 80) OR
(presentation > 75)) in the second IF statement will also be false, resulting in the
ELSE block being executed. The condition ((exam > 70) OR (presentation > 60)) in
the IF statement will be true, resulting in assigning a value of "C" to variable grade and
completing the execution of the code segment.

25. A team of programmers is designing software. One portion of the project presents a problem for which there is not
an obvious solution. After some research, the team determines that the problem is undecidable. Which of the
following best explains the consequence of the problem being undecidable?
(A) The problem can be solved algorithmically, but it will require an unreasonably long amount of time.
The problem can be solved algorithmically, but it will require an unreasonably large amount of data
(B)
storage.
(C) There is no possible algorithm that can be used to solve all instances of the problem.
There are several different possible algorithms that can solve the problem, but there is controversy about
(D)
which is the most efficient.

Answer C

Correct. An undecidable problem is one for which no algorithm can be constructed that is always capable
of providing a correct yes-or-no answer. There is no algorithmic solution that can solve all instances of
the problem.

Page 24 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

26. Consider the following code segment.

What are the contents of yourList after the code segment is executed?

(A) [10, 30, 50, 70]

(B) [20, 40, 60, 80]


(C) [10, 30, 50, 70, 20, 40, 60, 80]
(D) [20, 40, 60, 80, 10, 30, 50, 70]

Answer A

Correct. The last assignment statement assigns a copy of myList to yourList. Since myList
contains [10, 30, 50, 70], yourList will also contain [10, 30, 50, 70].

27. A city planner is using simulation software to study crowd flow out of a large arena after an event has ended. The
arena is located in an urban city. Which of the following best describes a limitation of using a simulation for this
purpose?
(A) The model used by the simulation software cannot be modified once the simulation has been used.
(B) The model used by the simulation software often omits details so that it is easier to implement.
Running a simulation requires more time to generate data from trials than observing the crowd exiting
(C)
the arena at various events.
Running a simulation requires a large number of observations to be collected before it can be used to
(D)
explore a problem.

Answer B

Correct. Simulations are limited by the model that is used. There may be many reasons for using a
simplified model, including ease of implementation.

AP Computer Science Principles Page 25 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

28. A student wants to determine whether a certain problem is undecidable. Which of the following will demonstrate
that the problem is undecidable?
Show that for one instance of the problem, an algorithm can be written that is always capable of
(A)
providing a correct yes-or-no answer.
Show that for one instance of the problem, no algorithm can be written that is capable of providing a
(B)
correct yes-or-no answer.
Show that for one instance of the problem, a heuristic is needed to write an algorithm that is capable of
(C)
providing a correct yes-or-no answer.
Show that for one instance of the problem, an algorithm that runs in unreasonable time can be written
(D)
that is capable of providing a correct yes-or-no answer.

Answer B

Correct. A decidable problem is one for which an algorithm can be constructed to produce a correct
output for all inputs. If, for one instance of the problem, this is not possible, then this problem cannot be
decidable. Therefore, it must be undecidable.

29. The following code segment is used to determine whether a customer is eligible for a discount on a movie ticket.

val1 (NOT (category = "new")) OR (age ≥ 65)


val2 (category = "new") AND (age < 12)

If category is "new" and age is 20, what are the values of val1 and val2 as a result of executing
the code segment?
(A) val1 = true, val2 = true
(B) val1 = true, val2 = false
(C) val1 = false, val2 = true

(D) val1 = false, val2 = false

Answer D

Correct. The variable val1 is assigned false because NOT (category = "new") is
false and age ≥ 65 is false. The variable val2 is assigned false because age < 12
is false.

Page 26 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

30. Consider the following code segment.

What is displayed as a result of executing the code segment?


(A) 1 3 5
(B) 5 3 1

(C) 100 300 500

(D) 500 300 100

Answer C

Correct. The code segment traverses the list from left to right and displays each value that is greater than
or equal to 90.

AP Computer Science Principles Page 27 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

31. Consider the following code segment.

Which of the following CANNOT be displayed as a result of executing the code segment?
(A) 1 1 1 1
(B) 1 2 3 2
(C) 1 2 3 4
(D) 1 3 2 4

Answer D

Correct. In the second iteration of the loop, i is equal to 2. Thus RANDOM(1, i) returns either 1
or 2. So, the output 1 3 2 4 is not possible.

Page 28 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

32. Consider the following procedure.

Procedure Call Explanation


drawCircle(xPos, Draws a circle on a coordinate grid with center (xPos, yPos)
yPos, rad) and radius rad

The drawCircle procedure is to be used to draw the following figure on a coordinate grid.

Let the value of the variable x be 2, the value of the variable y be 2, and the value of the variable r be
1. Which of the following code segments can be used to draw the figure?
drawCircle(x, y, r)
drawCircle(x, y + 2, r)
(A) drawCircle(x + 2, y, r)
drawCircle(x + 2, y + 2, r)
drawCircle(x, y, r)
drawCircle(x, y + 3, r)
(B) drawCircle(x + 3, y, r)
drawCircle(x + 3, y + 3, r)
drawCircle(x, y, r + 2)
drawCircle(x, y + 2, r + 2)
(C) drawCircle(x + 2, y, r + 2)
drawCircle(x + 2, y + 2, r + 2)
drawCircle(x, y, r + 3)
drawCircle(x, y + 3, r + 3)
(D) drawCircle(x + 3, y, r + 3)
drawCircle(x + 3, y + 3, r + 3)

AP Computer Science Principles Page 29 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. This code segment draws the four circles shown in the figure. Each circle has a radius of 1
unit. The circles are centered at (2, 2), (2, 5), (5, 2), and (5, 5).

33. Consider the following procedure.

Procedure Call Explanation


drawLine(x1, y1, Draws a line segment on a coordinate grid with endpoints at coordinates
x2, y2) (x1, y1) and (x2, y2)

The drawLine procedure is to be used to draw the following figure on a coordinate grid.

Let the value of the variable xVal be 6 and the value of the variable yVal be 5. Which of the following
code segments can be used to draw the figure?
drawLine(1, 5, xVal, yVal)
(A) drawline(1, 5, xVal, yVal + 2)
drawline(1, 5, xVal, yVal + 2)
drawLine(1, 5, xVal, yVal)
(B) drawline(1, 5, xVal, yVal + 2)
drawline(1, 5, xVal, yVal - 2)
drawLine(1, 5, xVal, yVal)
(C) drawline(1, 5, xVal + 2, yVal + 2)
drawline(1, 5, xVal + 2, yVal - 2)
drawLine(1, 5, xVal, yVal)
(D) drawline(1, 5, xVal + 2, yVal + 2)
drawline(1, 5, xVal - 2, yVal - 2)

Page 30 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. This code segment draws the three line segments shown in the figure. The first call to
drawLine draws a line segment with endpoints (1, 5) and (6, 5). The second call to
drawLine draws a line segment with endpoints (1, 5) and (6, 7). The third call to
drawLine draws a line segment with endpoints (1, 5) and (6, 3).

34. A graphic artist uses a program to draw geometric shapes in a given pattern. The program uses an algorithm that
draws the shapes based on input from the artist. The table shows the approximate number of steps the algorithm
takes to draw different numbers of shapes.

Number of Number of

Shapes Drawn Steps

4 17
5 24
6 35
7 50

Based on the values in the table, which of the following best characterizes the algorithm for drawing shapes,
where is a very large number?
The algorithm runs in a reasonable amount of time because it will use approximately steps to draw
(A)
shapes.
The algorithm runs in a reasonable amount of time because it will use approximately steps to draw
(B)
shapes.
The algorithm runs in an unreasonable amount of time because it will use approximately steps to draw
(C)
shapes.
The algorithm runs in an unreasonable amount of time because it will use approximately steps to
(D)
draw shapes.

Answer B

Correct. As increases, the number of steps is approximately equal to , which would make the
algorithm polynomial. An algorithm with an efficiency that approximates is said to run in a
reasonable amount of time.

AP Computer Science Principles Page 31 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

35. To attend a particular camp, a student must be either at least 13 years old or in grade 9 or higher, but must not yet be
18 years old. Let age represent a student’s age and let grade represent the student’s grade level. Which of the
following expressions evaluates to true if the student is eligible to attend the camp and evaluates to false
otherwise?
(A) ((age ≥ 13) OR (grade ≥ 9)) AND (age ≤ 18)
(B) ((age ≥ 13) OR (grade ≥ 9)) AND (age < 18)
(C) ((age ≥ 13) OR (grade ≥ 9)) OR (age ≤ 18)
(D) ((age ≥ 13) OR (grade ≥ 9)) OR (age < 18)

Answer B

Correct. This expression evaluates to true for students whose age is at least 13 or whose grade is at
least 9, and whose age is strictly less than 18.

36. Let n be an integer value. Which of the following expressions evaluates to true if and only if n is a two-digit
integer (i.e., in the range from 10 to 99, inclusive)?
(A) n = (n MOD 100)
(B) (n ≥ 10) AND (n < 100)
(C) (n < 10) AND (n ≥ 100)
(D) (n > 10) AND (n < 99)

Answer B

Correct. This expression evaluates to true if and only if the integer n is greater than or equal to 10
and strictly less than 100.

37. A student has a data file containing 10,000 numerical values. The student is writing a program to compute the
average of the numbers contained in the file. Which of the following procedures is most likely to be useful in the
student’s program?
(A) A procedure that returns the quotient when dividing one value by another
(B) A procedure that returns the sum when adding one value to another
(C) A procedure that returns the sum of the values in the file

(D) A procedure that returns true if the file contains any duplicate values and returns false otherwise

Page 32 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer C

Correct. The sum of the values in the file can be divided by the number of values in the file (10,000) to
compute the desired average.

38. A student is writing a program that is intended to replace each negative value in a particular column of a
spreadsheet with the value 0. Which of the following procedures is most likely to be useful in the student’s
program?
A procedure containsNegatives, which returns true if any negative values appear in the
(A)
column and returns false otherwise.
A procedure countNegatives, which returns the number of negative values that appear in the
(B)
column.
A procedure findNegative, which returns the row number of the first negative value that appears
(C)
in the column or -1 if there are no negative values.
(D) A procedure minimum, which returns the minimum value that appears in the column.

Answer C

Correct. Using this procedure would allow the student to identify the location where a negative value
occurs, and then change that value to 0. This process can continue with repeated calls to the procedure
until there are no more negative values.

39. Which of the following procedures would be most useful as part of a program to determine whether a word appears
in two different text files?
A procedure getWords, which takes a positive integer n and a text file as input and returns the first
(A)
n words in the text file.
A procedure isFound, which takes a word and a text file as input and returns true if the word
(B)
appears in the text file
A procedure textMatch, which takes two text files as input and returns true if the two text files
(C)
are identical.
A procedure sameSize, which takes two text files as input and returns true if the two text files
(D)
contain the same number of words.

Answer B

Correct. Two calls to this procedure can be used to determine if a particular word appears in each of two

AP Computer Science Principles Page 33 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

text files. If both calls return true, then the word appears in both text files.

40. Consider the following procedures for string manipulation.

Procedure Call Explanation


concat(str1, Returns a single string consisting of str1 followed by str2. For example,
str2) concat("key", "board") returns "keyboard".
Returns the reverse of the string str. For example, reverse("abcd") returns
reverse(str)
"dcba".

Which of the following code segments can be used to store "noon" in the string variable word ?
word "no"
(A) word concat(reverse(word), word)
word "no"
(B) word concat(reverse(word), reverse(word))
word "on"
(C) word concat(reverse(word), word)
word "on"
(D) word concat(reverse(word), reverse(word))

Answer C

Correct. This code segment assigns "on" to word. It then concatenates the reverse of word
("no") with word ("on"), producing the intended string "noon".

Page 34 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

41. Consider the following procedures for string manipulation.

Procedure Call Explanation


concat(str1, Returns a single string consisting of str1 followed by str2. For example,
str2) concat("key", "board") returns "keyboard".
Returns a substring of consecutive characters from str, starting with the
substring(str, character at position start and containing length characters. The first
start, length) character of str is located at position 1. For example,
substring("delivery", 3, 4) returns "live".
Returns the number of characters in str. For example, len("pizza")
len(str)
returns 5.

Assume that the string oldString contains at least 4 characters. A programmer is writing a code segment that is
intended to remove the first two characters and the last two characters from oldString and assign the result to
newString.

For example, if oldString contains "student", then newString should contain "ude".

Which of the following code segments can be used to assign the intended string to newString ?

Select two answers.

A newString substring(oldString, 3, len(oldString) - 4)

B newString substring(oldString, 3, len(oldString) - 2)

tempString substring(oldString, 3, len(oldString) - 2)


C newString substring(tempString, 1, len(tempString) - 2)
tempString1 substring(oldString, 1, 2)
D tempString2 substring(oldString, len(oldString) - 2, 2)
newString concat(tempString1, tempString2)

Answer A

Correct. To remove the first 2 characters of oldString, this code segment takes a substring starting
at position 3. To remove the last 2 characters of oldString, the substring ends at the position that is
4 characters less than the length of oldString (since 4 characters are removed).

AP Computer Science Principles Page 35 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer C

Correct. The first statement removes the first 2 characters of oldString and assigns the result to
tempString. The second statement removes the last 2 characters of tempString and assigns the
result to newString.

42. Consider the following procedures.

PROCEDURE proc1(str)
{
DISPLAY(str)
DISPLAY("happy")
}
PROCEDURE proc2(str1, str2)
{
proc1(str2)
DISPLAY(str1)
}

What is displayed as a result of the procedure call proc2("birthday", "to you") ?


(A) birthday happy to you
(B) birthday happy birthday
(C) to you birthday happy
(D) to you happy birthday

Answer D

Correct. The call to proc2 assigns "birthday" to the parameter str1 and "to you" to the
parameter str2. The first statement in proc2 calls proc1 and assigns "to you" to
proc1’s parameter str. The proc1 procedure displays the value of str ("to you") followed
by "happy", then terminates execution and returns to the point in proc2 immediately following
where proc1 was called. The proc2 procedure displays the value of str1 ("birthday") and
then terminates execution.

43. In which of the following scenarios would a simulation be the LEAST beneficial?

Page 36 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

An engineering company wants to test whether a change to a car design will negatively affect fuel
(A)
efficiency.
(B) An insurance company wants to study the effect of cold weather patterns on health-care costs.
A manufacturing company wants to determine whether using robots in its facility will increase
(C)
productivity.
A retail company wants to determine the most popular item that was sold on the company’s Web site
(D)
last month.

Answer D

Correct. The quantity of each item sold on the Web site can be counted. Therefore, the most popular item
can be determined without the use of a simulation.

44. For which of the following problems is using a simulation LEAST likely to be beneficial?

(A) Determining the longest word in a textbook


(B) Minimizing the customer wait times at a bank
(C) Predicting the outcomes of weather patterns
(D) Studying the formation of a galaxy

Answer A

Correct. This calculation does not need a simulation. Using standard algorithms that find a maximum
value from a list of values would be more appropriate.

45. In which of the following scenarios is using a simulation more beneficial than performing a calculation?

Select two answers.

A Determining the average grade of the students in a certain class

B Keeping track of the high score in a game

C Investigating ways to reduce the amount of trash in the ocean

D Studying the effect of a genetic change in a population

AP Computer Science Principles Page 37 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer C

Correct. Investigating ways to reduce the amount of trash in the ocean is too impractical for an
experiment. In this scenario, it would be more beneficial to use a simulation.

Answer D

Correct. Studying the effect of a genetic change in a population is too impractical for an experiment. In
this scenario, it would be more beneficial to use a simulation.

46. In the following statement, val1, val2, and result are Boolean variables.

Which of the following code segments produce the same result as the statement above for all possible values of
val1 and val2 ?

Select two answers.

Page 38 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

AP Computer Science Principles Page 39 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

47. Consider the following code segment.

What is the value of r as a result of executing the code segment?


(A) 10
(B) 20
(C) 30
(D) 40

Answer B

Correct. The first four statements assign values to the variables. The fifth statement assigns the value of
q (which is 20) to p. The sixth statement assigns the value of r (which is 30) to q. The
seventh statement assigns the value of q (which is 30) to s. The last statement assigns the value of
p (which is 20) to r. Therefore, r has the value 20. After executing the code segment, the value
of p is 20, the value of q is 30, the value of r is 20, and the value of s is 30.

48. In the following expression, the variable truckWeight has the value 70000 and the variable
weightLimit has the value 80000.

truckWeight < weightLimit

What value does the expression evaluate to?

Page 40 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) 70000
(B) 80000

(C) true

(D) false

Answer C

Correct. A relational expression evaluates to a Boolean value. The value of the variable truckWeight
is less than the value of the variable weightLimit, so the relational expression evaluates to true.

49. Consider the following code segment.

result 1
IF(score1 > 500)
{
result ← result + 1
IF(score2 > 500)
{
result ← result + 1
}
ELSE
{
result ← result + 2
}
}
ELSE
{
result ← result + 5
IF(score2 > 500)
{
result ← result + 1
}
ELSE
{
result ← result - 1
}
}

If the value of score1 is 350 and the value of score2 is 210, what will be the value of result after
the code segment is executed?

AP Computer Science Principles Page 41 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) 3
(B) 4

(C) 5

(D) 7

Answer C

Correct. When the code segment is executed, the condition in the outer IF statement evaluates to
false, resulting in the outer ELSE block being executed. Inside the ELSE block, result is
incremented by 5, making its value 6. The condition score2 > 500 in the embedded IF
statement evaluates to false, causing the ELSE block to be executed. The variable result is
decremented by 1, producing the final value 5.

50. Consider the following code segment.

What is the value of sum after the code segment is executed?


(A) 12
(B) 14

(C) 16

(D) 18

Page 42 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer C

Correct. The first three statements assign values to the variables. Since num1 < num2 evaluates to
false, the body of the ELSE block is executed and num3 is assigned the value 4. Since num2
≥ num3 evaluates to true, the body of the second IF block is executed and num1 is assigned
the value 8. Lastly, sum is assigned the value of 8 + 4 + 4, or 16.

51. Consider the following code segment.

What is the value of result after the code segment is executed?


(A) 6
(B) 10

(C) 15

(D) 21

Answer C

Correct. The variables x and result are initialized to 0. Inside the loop, result is increased
by x and x is increased by 1. The loop terminates when x exceeds 5. Therefore, result is
assigned the sum of the integers from 0 to 5, or 15.

AP Computer Science Principles Page 43 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

52. Consider the following code segment.

Which of the following initial values of the variable y would result in the variable z being set to 2 after the
code segment is executed?
(A) 1
(B) 2

(C) 3

(D) 4

Answer C

Correct. The remainder when 23 is divided by 3 is 2, so 23 MOD 3 is equal to 2.

53. Consider the following code segment.

What is displayed as a result of executing the code segment?


(A) true false false
(B) true false true
(C) true true false
(D) true true true

Page 44 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. At the beginning of the first iteration of the loop, the values of a, b, and c are true,
false, and true, respectively. At the end of the first iteration of the loop, the values of a, b, and
c are true, false, and false, respectively. At the end of the second (and last) iteration of the
loop, the values of a, b, and c are true, true, and true, respectively.

54. Consider the following code segment.

What is displayed as a result of executing the code segment?


(A) true true true
(B) false false false
(C) true false true
(D) false false true

Answer B

Correct. The first three statements assign values to the variables. The fourth statement assigns the value
of (NOT (a OR b)) AND c to a. Since a OR b is true, NOT (a OR b) is false, so
(NOT (a OR b)) AND c is false. The fifth statement assigns the value of c AND a to
c. Since a is now false, c AND a is false. The last three statements display the values of
the variables.

AP Computer Science Principles Page 45 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

55. Consider the following code segment.

theList [-2, -1, 0, 1, 2]


count1 0
count2 0
FOR EACH value IN theList
{
IF(value > 0)
{
count1 ← count1 + 1
}
ELSE
{
count2 ← count2 + 1
}
}

What are the values of count1 and count2 as a result of executing the code segment?
(A) count1 = 2, count2 = 2

(B) count1 = 2, count2 = 3

(C) count1 = 3, count2 = 2


(D) count1 = 5, count2 = 0

Answer B

Correct. The code segment iterates through each element in the list, incrementing count1 for each
positive value and incrementing count2 otherwise. The list contains two positive values, so count1
is 2 and count2 is 3.

56. Consider the following code segment.

What are the values of first and second as a result of executing the code segment?

Page 46 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) The value of first is true, and the value of second is true.

(B) The value of first is true, and the value of second is false.
(C) The value of first is false, and the value of second is true.
(D) The value of first is false, and the value of second is false.

Answer A

Correct. The first two statements assign values to the variables. The third statement assigns the value of
first (which is true) to second. The fourth statement assigns the value of second (which is
true) to first.

57. Consider the following code segment.

What is displayed as a result of executing the code segment?


(A) 10 20 30 40
(B) 21 30 40 50
(C) 21 40 30 40
(D) 21 40 30 50

AP Computer Science Principles Page 47 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. The first five statements assign values to the variables. The sixth statement assigns the value of
x + b (which is 40) to b. The seventh statement assigns the value of x + 1 (which is 21) to
a. The eighth statement assigns the value of c + d / 2 to d. According to the order of operations,
division has higher precedence than addition. Since c is 30 and d / 2 is 20, d is assigned the
value 50. The last four statements display the values of a, b, c, and d.

58. A computer scientist is analyzing four different algorithms used to sort a list. The table below shows the number of
steps each algorithm took to sort lists of different sizes.

Number of Steps Number of Steps Number of Steps Number of Steps

List Size

for Algorithm A for Algorithm B for Algorithm C for Algorithm D

1 10 2 1 1
2 20 4 2 4
3 30 8 6 9
4 40 16 24 16
5 50 32 120 25

Based on the values in the table, which of the algorithms appear to run in reasonable time?

Select two answers.

A Algorithm A

B Algorithm B

C Algorithm C

D Algorithm D

Answer A

Correct. As the size of the list grows, the number of steps needed to sort the list grows at a linear rate, as

Page 48 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

the number of steps is equal to for a list of size . This is an example of a polynomial efficiency and
indicates that the algorithm runs in a reasonable amount of time.

Answer D

Correct. The number of steps for this algorithm is equal to the length of the list squared, as the number of
steps is equal to for a list of size . This is an example of a polynomial efficiency and indicates that
the algorithm runs in a reasonable amount of time.

59. Consider the following code segment.

x 25
y 50
z 75
x y
y z
z x

Which of the variables have the value 50 after executing the code segment?
(A) x only
(B) y only

(C) x and z only

(D) x, y, and z

Answer C

Correct. The first three statements assign values to the variables. The fourth statement assigns the value
of y (which is 50) to x. The fifth statement assigns the value of z (which is 75) to y. The
sixth statement assigns the value of x (which is 50) to z. Therefore, x and z both have the value
50.

60. Suppose that a list of numbers contains values [-4, -1, 1, 5, 2, 10, 10, 15, 30]. Which of the
following best explains why a binary search should NOT be used to search for an item in this list?

AP Computer Science Principles Page 49 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A) The list contains both positive and negative elements.


(B) The elements of the list are not sorted.
(C) The list contains an odd number of elements.
(D) The list contains duplicate elements.

Answer B

Correct. In order for a binary search on a list to work, the elements of the list must be sorted.

61. In a certain video game, the variable maxPS represents the maximum possible score a player can earn. The
maximum possible score depends on the time it takes the player to complete the game. The value of maxPS
should be 30 if time is greater than 120 and 50 otherwise.

Which of the following code segments correctly sets the value of maxPS based on the value of time ?

Select two answers.

Page 50 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer A

Correct. This code segment sets maxPS to 50 by default, then uses the IF statement to set maxPS
to 30 when time > 120.

Answer D

Correct. This code segment uses the IF statement to set maxPS to 30 when time > 120 and
uses the ELSE statement to set maxPS to 50 otherwise.

AP Computer Science Principles Page 51 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

62. In a certain game, a player may have the opportunity to attempt a bonus round to earn extra points. In a typical
game, a player is given 1 to 4 bonus round attempts. For each attempt, the player typically earns the extra points
70% of the time and does not earn the extra points 30% of the time.

The following code segment can be used to simulate the bonus round.

Which of the following is NOT a possible output of this simulation?


The player had 1 bonus round attempts and 1 of them earned extra
(A)
points.
The player had 2 bonus round attempts and 0 of them earned extra
(B)
points.
The player had 3 bonus round attempts and 7 of them earned extra
(C)
points.
The player had 4 bonus round attempts and 3 of them earned extra
(D)
points.

Answer C

Correct. Since attempts is assigned a random value between 1 and 4, it is possible for the value
of attempts to be 3. However, it is not possible for the value of success to be 7 because the
loop only iterates three times.

Page 52 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

63. The transportation department plans to build a new high-speed train route between two cities. The transportation
department wants to implement a simulation of the train before any construction begins on this project. Which of
the following statements is true about the use of a simulation for this project?
(A) A simulation cannot be used to test the train under varying weather conditions.
(B) Implementing a simulation is likely to increase the overall costs associated with the project.
(C) Other high-speed train routes have been constructed in other locations, so a simulation is not needed.
(D) Using a simulation may expose potential safety issues that can be corrected before construction begins.

Answer D

Correct. Simulations are most useful when real-world events are impractical for experiments.
Discovering and correcting potential safety issues using a simulation before the train route is constructed
is much safer and less expensive than discovering them after the train route has been constructed.

AP Computer Science Principles Page 53 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

64. Consider the following spinner, which is used to determine how pieces are to be moved on a game board. Each
region is of equal size.

Which of the following code segments can be used to simulate the behavior of the spinner?

Page 54 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

(D)

AP Computer Science Principles Page 55 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. Of the regions in the spinner, 25% are yellow, 25% are blue, and 50% are red. This code
segment generates a random value from 1 to 4, inclusive. If the random value is 1 (25% of the
time), color is assigned "yellow". If the random value is 2 (25% of the time), color is
assigned "blue". Otherwise (50% of the time), color is assigned "red".

65. Consider the following code segment, which is intended to store ten consecutive even integers, beginning with
2, in the list evenList. Assume that evenList is initially empty.

i 1
REPEAT 10 TIMES
{
<MISSING CODE>
}

Which of the following can be used to replace <MISSING CODE> so that the code segment works as intended?
APPEND(evenList, i)
(A) i i + 2
i i + 2
(B) APPEND(evenList, i)

APPEND(evenList, 2 * i)
(C) i i + 1
i i + 1
(D) APPEND(evenList, 2 * i)

Answer C

Correct. For the first iteration of the loop, twice the value of i, or 2, is appended to
evenList, and then i is incremented to 2. For the second iteration of the loop, twice the value of
i, or 4, is appended to the list, and then i is incremented to 3. This continues eight more times,
appending the next eight even numbers to evenList. This code segment will generate the list [2,
4, 6, 8, 10, 12, 14, 16, 18, 20].

66. Which of the following code segments can be used to interchange the values of the variables num1 and num2 ?

Page 56 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

(D)

Answer D

Correct. The first statement assigns the value of num1 to the temporary variable temp. The second
statement assigns the value of num2 to num1. The third statement assigns the original value of
num1, which is stored in temp, to num2. The original values of num1 to num2 are
interchanged.

AP Computer Science Principles Page 57 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

67. The figure below shows a circuit composed of two logic gates. The output of the circuit is true.

Which of the following is a true statement about input A?

(A) Input A must be true.


(B) Input A must be false.
(C) Input A can be either true or false.
(D) There is no possible value of input A that will cause the circuit to have the output true.

68. A spinner contains 12 regions of equal size. The regions are numbered 1 to 12. Which of the following code
segments can be used to simulate the results of spinning the spinner three times and assigns the sum of the values
obtained by the three spins to the variable sum ?

(A) sum RANDOM(1, 12) + RANDOM(1, 12) + RANDOM(1, 12)

(B) sum RANDOM(1, 36)


(C) sum 3 * RANDOM(1, 12)
(D) sum 12 * RANDOM(1, 3)

Answer A

Correct. The first spin is simulated by the first call to RANDOM, which returns a random integer

Page 58 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

between 1 and 12, inclusive. Similarly, the second and third spins are simulated by the second and
third calls to RANDOM, producing a total of three integer values. The sum of the three values is assigned
to sum.

69. A teacher stores the most recent quiz scores for her class in the list scores. The first element in the list holds the
maximum possible number of points that can be awarded on the quiz, and each remaining element holds one
student’s quiz score. Assume that scores contains at least two elements. Which of the following code segments
will set the variable found to true if at least one student scored the maximum possible number of points on
the quiz and will set found to false otherwise?

AP Computer Science Principles Page 59 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

Page 60 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(D)

Answer A

Correct. This code segment traverses the list beginning with the second element, so it is correctly
comparing only student scores to the maximum possible score, which is found by accessing
scores[1]. The variable found will only be set to true when a student’s score equals the
maximum possible score. The code also sets the number of iterations to LENGTH(scores) - 1 to
reflect that the first list element (maximum score) is skipped.

70. Three words are stored in the variables word1, word2, and word3. The values of the variables are to be
updated as shown in the following table.

Value Before Value After


Variable
Updating Updating
word1 "xylophone" "zebra"
word2 "yarn" "yarn"
word3 "zebra" "xylophone"

Which of the following code segments can be used to update the values of the variables as shown in the table?

AP Computer Science Principles Page 61 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

temp word1
(A) word3 word1
word1 temp
temp word1
(B) word1 word3
word3 temp
temp word1
word1 word2
(C) word2 word3
word3 temp
temp word3
word3 word2
(D) word2 word1
word1 temp

Answer B

Correct. The first statement assigns the value of word1 to the temporary variable temp. The second
statement assigns the value of word3 to word1. The third statement assigns the original value of
word1, which is stored in temp, to word3. The original values of word1 and word3 are
interchanged, which reverses the values of the variables as intended.

Page 62 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

71. Consider the following procedure.

Procedure Call Explanation


drawCircle(xPos, yPos, Draws a circle on a coordinate grid with center (xPos, yPos)
rad) and radius rad

The drawCircle procedure is to be used to draw the following figure on a coordinate grid.

Which of the following code segments can be used to draw the figure?

Select two answers.

AP Computer Science Principles Page 63 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

x 4
y 1
r 0
REPEAT 3 TIMES
A {
drawCircle(x, y, r)
r ← r + 1
y ← y + 1
}
x 4
y 1
r 0
REPEAT 3 TIMES
B {
r ← r + 1
y ← y + 1
drawCircle(x, y, r)
}
x 4
y 4
r 3
REPEAT 3 TIMES
C {
drawCircle(x, y, r)
y ← y - 1
r ← r - 1
}
x 4
y 4
r 3
REPEAT 3 TIMES
D {
y ← y - 1
r ← r - 1
drawCircle(x, y, r)
}

Page 64 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

72. Consider the following procedures for string manipulation.

Procedure Call Explanation


concat(str1, Returns a single string consisting of str1 followed by str2. For example,
str2) concat("key", "board") returns "keyboard".
Returns the first length characters of str or str if length is greater
prefix(str, than the number of characters in str. For example, prefix("delivery",
length) 3) returns "del" and prefix("delivery", 100) returns
"delivery".

The variable initials is to be assigned a string consisting of the first letter of the string firstName
followed by the first letter of the string lastName. Which of the following assigns the correct string to
initials ?

(A) initials concat(prefix(firstName, 1), prefix(lastName, 1))

(B) initials concat(prefix(firstName, 2), prefix(lastName, 2))


(C) initials prefix(concat(firstName, lastName), 1)
(D) initials prefix(concat(firstName, lastName), 2)

Answer A

Correct. This statement will correctly form the initials. It uses calls to prefix to obtain the first letters
of each name, then uses a call to concat to concatenate the two letters in the correct order.

73. A student is developing a program that allows users to look up the definitions of words that appear in a book.

The student plans to perform a large number of searches through a dictionary containing words and their definitions.
The student will use a procedure written by a computer scientist to quickly search the dictionary (and knows that the
procedure will return a definition if one is available). The student cannot modify the search procedure written by the
computer scientist but can call the procedure by supplying a word.

Which of the following is a true statement about the student’s use of the computer scientist’s search procedure?
(A) The student is changing the search procedure’s internal abstractions.
The student is modifying the search procedure to take a definition as an argument and return the
(B)
corresponding word.
The student is reusing the computer scientist’s procedural abstraction by knowing what the procedure
(C)
does without knowing how it does it.
The student is reusing the computer scientist’s procedural abstraction by using duplicate code each time
(D)
a search needs to occur.

AP Computer Science Principles Page 65 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer C

Correct. The student knows that the procedure searches for a value without knowing how it does the
searching. This is an example of the use of procedural abstraction.

74. Consider the following procedures, which are used to control a device that draws lines on paper.

Procedure Call Explanation


penDown() Places the device’s pen on the paper so that a line is drawn when the device moves
Lifts the device’s pen off of the paper so that no line is drawn when the device
penUp()
moves
goForward(x) Moves the device forward x units
turnRight(x) Rotates the device in place x degrees clockwise (i.e., makes an in-place right turn)

Consider the goal of using the device to produce the following drawing, where each line shown has a length of 10
units and the horizontal lines are 10 units apart.

The device is positioned at the upper-left corner of a sheet of paper and is facing right. Which of the following code
segments will produce the drawing shown?

Page 66 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

penDown()
goForward(10)
turnRight(90)
(A) goForward(10)
turnRight(90)
goForward(10)
penDown()
goForward(10)
penUp()
(B) turnRight(90)
turnRight(90)
penDown()
goForward(10)
penDown()
goForward(10)
turnRight(90)
(C) penUp()
goForward(10)
turnRight(90)
penDown()
penDown()
goForward(10)
penUp()
turnRight(90)
(D) goForward(10)
turnRight(90)
penDown()
goForward(10)

Answer D

Correct. This code segment draws a horizontal line, raises the pen, moves the device 10 units down and
turns it to face left, then draws the second horizontal line.

75. A large number of genetic codes are stored as binary values in a list. Which one of the following conditions must be
true in order for a researcher to obtain the correct result when using a binary search algorithm to determine if a
given genetic code is in the list?
(A) The genetic codes must be converted from binary to decimal numbers.
(B) The list must be sorted based on the genetic code values.
(C) The number of genetic code values in the list must be a power of 2.
(D) The number of genetic code values in the list must be even.

AP Computer Science Principles Page 67 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. In order for a binary search on a list to work as intended, the list must be sorted.

76. The code segment below uses the procedure IsFound (list, item), which returns true if item appears in list and
returns false otherwise. The list resultList is initially empty.

Which of the following best describes the contents of resultList after the code segment is executed?
(A) All elements in inputList1 followed by all elements in inputList2
(B) Only elements that appear in both inputList1 and inputList2
(C) Only elements that appear in either inputList1 or inputList2 but not in both lists
(D) Only elements that appear in inputList1 but not in inputList2

Answer B

This option is correct. Each item in inputList1 is checked to see if it appears in inputList2. If the item
appears in inputList2, the item is appended to resultList. Since resultList is initially empty, at the end of
the FOR EACH loop resultList will contain precisely the elements that appear in both initial lists.

77. The list wordList contains a list of 10 string values. Which of the following is a valid index for the list?
(A) -1
(B) "hello"
(C) 2.5

(D) 4

Page 68 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. List indices are typically nonnegative integers. The exam reference sheet describes a list
structure whose index values are 1 through the number of elements in the list, inclusive. So, 4 would
be considered a valid index.

78. Consider the following procedure.

PROCEDURE doSomething(num1, num2)


{
DISPLAY(num1)
RETURN(num1)
DISPLAY(num2)
}

Consider the following statement.

DISPLAY(doSomething(10, 20))

What is displayed as a result of executing the statement above?


(A) 10 10
(B) 10 20
(C) 10 10 20
(D) 10 20 10

Answer A

Correct. The first statement in the doSomething procedure displays the value of num1, or
10. The second statement returns the value 10 and terminates execution of the procedure. The
returned value 10 is displayed.

AP Computer Science Principles Page 69 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

79. Consider the following code segment.

If the value of x is 3 and the value of y is 5, what is displayed as a result of executing the code segment?

(A) -2

(B) 2
(C) 8
(D) Nothing will be displayed.

Answer A

Correct. Since 3 < 5, the program will execute the ELSE code block and will print x - y = 3
- 5 = -2.

80. To qualify for a particular scholarship, a student must have an overall grade point average of 3.0 or above and must
have a science grade point average of over 3.2. Let overallGPA represent a student’s overall grade point
average and let scienceGPA represent the student’s science grade point average. Which of the following
expressions evaluates to true if the student is eligible for the scholarship and evaluates to false otherwise?
(A) (overallGPA > 3.0) AND (scienceGPA > 3.2)
(B) (overallGPA > 3.0) AND (scienceGPA ≥ 3.2)
(C) (overallGPA ≥ 3.0) AND (scienceGPA > 3.2)
(D) (overallGPA ≥ 3.0) AND (scienceGPA ≥ 3.2)

Answer C

Correct. This expression will evaluate to true if and only if the student’s overall GPA is greater than
or equal to 3.0 and the student’s science GPA is strictly greater than 3.2.

Page 70 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

81. For which of the following lists can a binary search be used to search for an item in the list?

I. ["blue", "green", "jade", "mauve", "pink"]


II. [5, 5, 5, 5, 6, 7, 8, 8, 8]
III. [10, 5, 3, 2, -4, -8, -9, -12]
(A) I only
(B) III only
(C) I and III only
(D) I, II, and III

Answer D

Correct. In order for a binary search on a list to work, the list must be sorted. All three lists are sorted
(the first one in alphabetical order, the third one in reverse order), so a binary search is appropriate for all
of them.

82. Which of the following best explains how algorithms that run on a computer can be used to solve problems?
(A) All problems can be solved with an algorithm that runs in a reasonable amount of time.
All problems can be solved with an algorithm, but some algorithms might need a heuristic to run in a
(B)
reasonable amount of time.
All problems can be solved with an algorithm, but some algorithms might run in an unreasonable
(C)
amount of time.
(D) Some problems cannot be solved by an algorithm.

Answer D

Correct. Undecidable problems cannot be solved with an algorithm.

AP Computer Science Principles Page 71 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

83. Consider the following code segment with an integer variable num.

IF(num > 0)
{
DISPLAY("positive")
}
IF(num < 0)
{
DISPLAY("negative")
}
IF(num = 0)
{
DISPLAY("zero")
}

Which of the following code segments is equivalent to the code segment above?

Page 72 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

IF(num < 0)
{
DISPLAY("negative")
}
ELSE
{
(A) DISPLAY("positive")
}
IF(num = 0)
{
DISPLAY("zero")
}
IF(num < 0)
{
DISPLAY("negative")
}
ELSE
{
IF(num = 0)
(B) {
DISPLAY("zero")
}
ELSE
{
DISPLAY("positive")
}
}
IF(num ≤ 0)
{
DISPLAY("negative")
}
ELSE
{
IF(num = 0)
(C) {
DISPLAY("zero")
}
ELSE
{
DISPLAY("positive")
}
}
IF(num ≤ 0)
{
DISPLAY("negative")
}
IF(num = 0)
{
(D) DISPLAY("zero")
}
ELSE
{
DISPLAY("positive")
}

AP Computer Science Principles Page 73 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer B

Correct. The given code segment displays "positive" when num is positive, displays
"negative" when num is negative, and displays "zero" when num is 0. This code segment
produces the same result. When num is negative, "negative" is displayed. Otherwise, when num
is 0, "zero" is displayed. Otherwise, "positive" is displayed.

84. A computer program contains code in several places that asks a user to enter an integer within a specified range of
values. The code repeats the input request if the value that the user enters is not within the specified range. A
programmer would like to create a procedure that will generalize this functionality and can be used throughout the
program. The correct use of the procedure is shown below, where min is the least acceptable value, max is the
greatest acceptable value, and promptString is the string that should be printed to prompt the user enter a
value.

inputVal getRange(min, max, promptString)

Which of the following is a correct implementation of the getRange procedure?

Page 74 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

PROCEDURE getRange(min, max, promptString)


{
(A) DISPLAY(promptString)
RETURN(INPUT())
}
PROCEDURE getRange(min, max, promptString)
{
DISPLAY(promptString)
(B) x ← INPUT()
RETURN(x)
}
PROCEDURE getRange(min, max, promptString)
{
DISPLAY(promptString)
x ← INPUT()
REPEAT UNTIL(x ≥ min AND x ≤ max)
(C) {
DISPLAY(promptString)
x ← INPUT()
}
}
PROCEDURE getRange(min, max, promptString)
{
DISPLAY(promptString)
x ← INPUT()
REPEAT UNTIL(x ≥ min AND x ≤ max)
(D) {
DISPLAY(promptString)
x ← INPUT()
}
RETURN(x)
}

Answer D

Correct. This implementation prompts the user and accepts the user’s input. For as long as the user’s
input is outside of the valid range, the program repeats the prompt and the call to the INPUT procedure.
When the loop terminates, the last input value is returned.

AP Computer Science Principles Page 75 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

85. A game program contains the following code to update three score variables, health, food, and
knowledge. The maximum values for the three variables are 100, 80, and 25, respectively.

health health + 10
IF(health > 100)
{
health ← 100
}
food food + 1
IF(food > 80)
{
food ← 80
}
knowledge knowledge + 5
IF(knowledge > 25)
{
knowledge ← 25
}

The game program’s author would like to write a procedure that could be used to update any variable in the
game (myScore) that has a maximum value (myLimit) by a given amount (myAmount). A correct call of
the procedure is shown below.

myScore updateScore(myScore, myAmount, myLimit)

Which of the following is a correct implementation of updateScore ?

Page 76 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

PROCEDURE updateScore(score, amount, limit)


{
score ← score + amount
IF (score > limit)
(A) {
score ← limit
}
}
PROCEDURE updateScore(score, amount, limit)
{
score ← score + amount
IF(score > amount)
(B) {
score ← limit
}
RETURN(limit)
}
PROCEDURE updateScore(score, amount, limit)
{
score ← score + amount
IF(score > limit)
(C) {
score ← limit
}
RETURN(score)
}
PROCEDURE updateScore(score, amount, limit)
{
IF(score = health)
{
score ← score + 10
}
IF(score = food)
{
score ← score + 1
(D) }
IF(score = knowledge)
{
score ← score + 5
}
IF(score > limit)
{
score ← limit
}
}

Answer C

Correct. This procedure adds the correct amount to the score to be updated. If the score to be updated is

AP Computer Science Principles Page 77 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

larger than the limit, the score’s value is set to the limit. The updated value is returned.

86. A student wrote the following procedure to calculate the sum of the integers from 1 to 5.

The student later decides to modify the procedure to calculate the sum of the integers from 1 to max, which
represents any positive integer greater than 1.

Which of the following changes should be made to the procedure to meet the student’s goal?

I. The procedure should take max as an input parameter.


II. The condition in the REPEAT UNTIL block should be changed to count > max.
III. The condition in the REPEAT UNTIL block should be changed to max < 5.
(A) I only
(B) II only
(C) I and II
(D) I and III

Answer C

Correct. Statement I is correct because a parameter is needed to generalize the functionality to any
possible value of max. Statement II is correct because the loop should repeat until count exceeds
the maximum value to be included in the sum. Statement III is incorrect because max should be
compared to count, not to 5.

Page 78 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

87. Shoppers at a mall were asked whether they preferred wearing gloves or mittens in cold weather. Shoppers’
preferences were stored in the list voteList as strings, with the string "Gloves" representing a preference
for gloves and the string "Mittens" representing a preference for mittens.

The following code segment is intended to traverse the list and display the number of shoppers who chose gloves
and the number of shoppers who chose mittens.

numGlovesVotes 0
numMittensVotes 0
<MISSING CODE>
{
IF(vote = "Gloves")
{
numGlovesVotes ← numGlovesVotes + 1
}
ELSE
{
numMittensVotes ← numMittensVotes + 1
}
}
DISPLAY(numGlovesVotes)
DISPLAY(" shoppers chose gloves and")
DISPLAY(numMittensVotes)
DISPLAY(" shoppers chose mittens.")

Which of the following should replace <MISSING CODE> so that the code segment works as intended?
(A) IF(vote ≤ LENGTH(voteList))
(B) FOR EACH vote IN voteList
(C) REPEAT LENGTH(voteList) TIMES
(D) REPEAT UNTIL(vote > LENGTH(voteList))

Answer B

Correct. Traversing a list using the FOR EACH item IN aList statement accomplishes two tasks.
It does a complete traversal of the list and assigns to the variable item the value of each element of
aList sequentially, from the first element to the last element. The list being traversed is
voteList. In the statement IF(vote = "Gloves"), vote is the variable being used to
represent the value of each element in the list. Therefore, FOR EACH vote IN voteList is
correct.

AP Computer Science Principles Page 79 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

88. Consider the following code segment. Assume that index1 is a number between 1 and
LENGTH(theList), inclusive, and index2 is a number between 2 and LENGTH(theList) -
1, inclusive.

theList [9, -1, 5, 2, 4, 8]


x theList[index1] + theList[index2]

What is the largest possible value that the variable x can have after the code segment executes?
(A) 17

(B) 14

(C) 11
(D) 4

Answer B

Correct. The value of index1 can be any number between 1 and 6, inclusive. The value of
index2 can be any number between 2 and 5, inclusive. The largest values in theList in those
index ranges occur when index1 is equal to 1 and index2 is equal to 3. Therefore, the largest
value that can be assigned to x is 9 + 5, or 14.

Page 80 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

89. The following grid contains a robot represented as a triangle, which is initially facing right.

The following code segment is intended to move the robot to the gray square.

<MISSING STATEMENT>
{
REPEAT 4 TIMES
{
MOVE_FORWARD()
ROTATE_RIGHT()
}
ROTATE_LEFT()
MOVE_FORWARD()
ROTATE_RIGHT()
}

Which of the following can be used as a replacement for <MISSING STATEMENT> so that the code segment
works as intended?
(A) REPEAT 1 TIMES
(B) REPEAT 2 TIMES
(C) REPEAT 3 TIMES
(D) REPEAT 4 TIMES

Answer B

Correct. The loop that repeats four times returns the robot to its starting position. The three lines of code
that follow the loop move the robot one row up and leave it facing right. When this has been repeated
twice, the robot is in the gray square.

AP Computer Science Principles Page 81 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

90. The following grid contains a robot represented as a triangle, which is initially in the bottom-left square of the grid
and facing the top of the grid. The robot can move into a white or a gray square but cannot move into a black
region.

The following code segment implements an algorithm that moves the robot from its initial position to the gray
square and facing the top of the grid.

When the robot reaches the gray square, it turns around and faces the bottom of the grid. Which of the following
changes, if any, should be made to the code segment to move the robot back to its original position in the bottom-
left square of the grid and facing toward the bottom of the grid?
(A) Interchange the ROTATE_RIGHT and the ROTATE_LEFT blocks.
(B) Replace ROTATE_RIGHT with ROTATE_LEFT.
(C) Replace ROTATE_LEFT with ROTATE_RIGHT.

(D) No change is needed; the algorithm is correct as is.

Answer D

Correct. In order for the robot to move from the gray square back to its original position, it must move

Page 82 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

forward two squares, turn right, move forward four squares, turn left, and move forward two squares.
The same set of moves can be used in both directions, so no change is needed to the algorithm.

91. The following question uses a robot in a grid of squares. The robot is represented as a triangle, which is initially
facing toward the top of the grid.

The following code segment moves the robot around the grid. Assume that n is a positive integer.

Line 1: count 0
Line 2: REPEAT n TIMES
Line 3: {
Line 4: REPEAT 2 TIMES
Line 5: {
Line 6: MOVE_FORWARD()
Line 7: }
Line 8: ROTATE_RIGHT()
Line 9: }

Consider the goal of modifying the code segment to count the number of squares the robot visits before execution
terminates. Which of the following modifications can be made to the code segment to correctly count the number of
squares the robot moves to?
(A) Inserting the statement count count + 1 between line 6 and line 7

(B) Inserting the statement count count + 2 between line 6 and line 7
(C) Inserting the statement count count + 1 between line 8 and line 9
(D) Inserting the statement count count + n between line 8 and line 9

Answer A

Correct. Inserting this statement between lines 6 and 7 increases the value of count once each time

AP Computer Science Principles Page 83 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

the robot moves forward, which keeps an accurate count of the number of squares the robot visits.

92. The following grid contains a robot represented as a triangle, which is initially facing toward the top of the grid. The
robot can move into a white or gray square but cannot move into a black region.

Which of the following code segments can be used to move the robot to the gray square?

Page 84 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

REPEAT 3 TIMES
{
MOVE_FORWARD()
}
REPEAT 2 TIMES
{
(A) MOVE_FORWARD()
}
REPEAT 3 TIMES
{
MOVE_FORWARD()
}
REPEAT 8 TIMES
{
(B) MOVE_FORWARD()
}
REPEAT 3 TIMES
{
MOVE_FORWARD()
}
ROTATE_LEFT()
REPEAT 2 TIMES
{
(C) MOVE_FORWARD()
}
ROTATE_LEFT()
REPEAT 3 TIMES
{
MOVE_FORWARD()
}
REPEAT 3 TIMES
{
MOVE_FORWARD()
}
ROTATE_LEFT()
REPEAT 2 TIMES
{
(D) MOVE_FORWARD()
}
ROTATE_RIGHT()
REPEAT 3 TIMES
{
MOVE_FORWARD()
}

Answer D

Correct. The robot moves forward three spaces, turns to the left, moves forward two spaces, turns to the
right, and moves forward three spaces.

AP Computer Science Principles Page 85 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

93. Consider the following code segment.

firstList ["guitar", "drums", "bass"]


secondList ["flute", "violin"]
thirdList []
thirdList firstList
firstList secondList
secondList thirdList

What are the contents of secondList after the code segment is executed?
(A) []

(B) ["guitar", "drums", "bass"]

(C) ["flute", "violin"]


(D) ["flute", "violin", "guitar", "drums", "bass"]

Answer B

Correct. The code segment initializes firstList to contain ["guitar", "drums",


"bass"], secondList to contain ["flute", "violin"], and thirdList to contain an
empty list. The code segment then swaps the contents of firstList and secondList by
assigning firstList to thirdList, then secondList to firstList, then
thirdList to secondList.

94. Three teams (Team A, Team B, and Team C) are participating in a trivia contest. Let scoreA represent the
number of correct questions for Team A, scoreB represent the number of correct questions for Team B, and
scoreC represent the number of correct questions for Team C. Assuming no two teams get the same number of
correct questions, which of the following code segments correctly displays the team with the highest number of
correct questions?

Page 86 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

(A)

(B)

(C)

AP Computer Science Principles Page 87 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

(D)

Answer A

Correct. If scoreA is greater than both scoreB and scoreC (the first two IF clauses), then
Team A wins. If scoreA is greater than scoreB but not greater than scoreC, then scoreC is
greater than both scoreA and scoreB and Team C wins. If scoreB is greater than scoreA
(the outer ELSE clause) and scoreC (the IF clause in the outer ELSE), then Team B wins. If
scoreB is greater than scoreA but not greater than scoreC, then scoreC is greater than both
scoreA and scoreB and Team C wins.

Page 88 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

95. A numeric test score is to be converted to a letter grade of A, B, or C according to the following rules: A score
greater than 90 is considered an A; a score between 80 and 90, inclusive, is considered a B; and any other score is
considered a C.

Which of the following code segments will assign the correct letter grade to grade based on the value of the
variable score ?

I.

II.

III.

(A) II only
(B) I and II only
(C) I and III only
(D) II and III only

AP Computer Science Principles Page 89 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. Code segment I does not work correctly because it is not possible for "C" to be the value of
grade at the end of the code segment. Code segment II correctly assigns "A" when the numeric
score is greater than 90, or "B" if the numeric score is not greater than 90 but is greater than or
equal to 80, or "C" otherwise. Code segment III assigns "C" when the numeric score is less than
80, or "B" if the numeric score is not less than 80 but is less than or equal to 90, or "A"
otherwise.

96. A computer science student completes a program and asks a classmate for feedback. The classmate suggests
rewriting some of the code to include more procedural abstraction. Which of the following is NOT a benefit of
procedural abstraction?
(A) Making the code more readable
(B) Making the code run faster
(C) Providing more opportunities for code reuse
(D) Reducing the amount of duplicated code

Answer B

Correct. Modularity and code reuse do not necessarily lead to faster-running code.

Page 90 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

97. Consider the following code segment with integer variables x and y.

If x has a value of 7 and y has a value of 20, what is displayed as a result of executing the code segment?
(A) ONE
(B) TWO

(C) THREE

(D) FOUR

Answer C

Correct. The value "THREE" is displayed whenever x is less than or equal to 10 and y is greater
than 3.

AP Computer Science Principles Page 91 of 93


Scoring Guide

AP-CSP-BigIdea3-Assignment

98. In the following procedure, the parameter str is a string and the parameter num is a number.

PROCEDURE printArgs(str, num)


{
DISPLAY(num)
DISPLAY(str)
DISPLAY(num)
}

Consider the following code segment.

printArgs("**", 1)
printArgs("*", 2)

What is displayed as a result of executing the code segment?


(A) 1 * 1 2 ** 2

(B) 1 ** 1 2 * 2

(C) * 1 * ** 2 **
(D) ** 1 ** * 2 *

Answer B

Correct. The first call to printArgs displays the number 1, followed by the string
"**", followed by the number 1. The second call to printArgs displays the number
2, followed by the string "*", followed by the number 2.

99. Consider the following code segment.

Which of the following describes the possible values of ans as a result of executing the code segment?
(A) Any integer value from 1 to 8, inclusive
(B) Any integer value from 1 to 16, inclusive
(C) Any integer value from 4 to 8, inclusive

(D) Any integer value from 7 to 16, inclusive

Page 92 of 93 AP Computer Science Principles


Scoring Guide

AP-CSP-BigIdea3-Assignment

Answer D

Correct. The first call to RANDOM returns a random integer from 1 to 3, inclusive. The second call
to RANDOM returns a random integer from 2 to 5, inclusive. The third call to RANDOM returns a
random integer from 4 to 8, inclusive. Therefore, the least possible sum is 1 + 2 + 4, or
7, and the greatest possible sum is 3 + 5 + 8, or 16.

AP Computer Science Principles Page 93 of 93

You might also like