APCSP Test Study Guide
3.5 Boolean Expressions 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?
((age ≥ 13) OR (grade ≥ 9)) AND (age < 18)
3.5 Boolean Expressions 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)?
(n ≥ 10) AND (n < 100)
3.5 Boolean Expressions 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?
(overallGPA ≥ 3.0) AND (scienceGPA > 3.2)
3.8 Iteration Consider the following code segment. x ← 0 result ← 0 REPEAT UNTIL [x > 5] result ← result + x x ← x + 1 What is the value of result after the code segment is executed?
15
3.6 Conditionals Consider the following code segment. num1 ← 6 num2 ← 4 num3 ← 10 IF [num1 < num2] num1 ← num2 ELSE num3 ← num2 IF [num2 >= num3] num1 ← num2 plus num3 sum ← num1 + num2 + num3 What is the value of sum after the code segment is executed?
16
3.1 Variables and Assignments p ← 10 q ← 20 r ← 30 s ← 40 p ← q q ← r s ← q r ←p What is the value of r as a result of executing the code segment?
20
3.3 Mathematical Expressions Consider the following code segment. a ← 10 b ← 20 c ← 30 d ← 40 x ← 20 b ← x + b a ← x + 1 d ← c + d / 2 DISPLAY a DISPLAY b DISPLAY c DISPLAY d What is displayed as a result of executing the code segment?
21 40 30 50
3.3 Mathematical Expressions Consider the following code segment. x ← 23 y ← x MOD y 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?
3
3.7 Nested Conditionals 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?
5
3.1 Variables and Assignments 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 string variable named studentName and a Boolean variable named isAbsent
3.7 Nested Conditionals 3 teams (Team A, Team B, and Team C) are participating in a trivia contest. scoreA represents 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? A. IF [scoreA > scoreB] IF [scoreA > scoreC] DISPLAY ["Team A Wins"] ELSE [scoreA > scoreC] DISPLAY ["Team C Wins"] ELSE IF [scoreB > scoreC] DISPLAY ["Team B Wins"] ELSE DISPLAY ["Team C Wins"] B. IF [scoreA > scoreB] IF [scoreB > scoreC] DISPLAY ["Team A Wins"] ELSE DISPLAY ["Team C Wins"] ELSE IF [scoreB > scoreC] DISPLAY ["Team B Wins"] ELSE DISPLAY ["Team C Wins"]
A.
3.6 Conditionals 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 ?
A., D.
3.7 Nested Conditionals 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 ? A. IF(score > 100) { bonus ← score * 10 } ELSE { IF(score ≥ 50) { bonus ← score } ELSE { bonus ← 0 } } B. IF(score ≥ 50) { IF(score > 100) { bonus ← score * 10 } ELSE { bonus ← 0 } } ELSE { bonus ← score } C. IF(score < 50) { bonus ← 0 } ELSE { IF(score ≥ 50) { bonus ←score } ELSE { bonus ←score * 10 } } D. IF(score < 50) { bonus ← 0 } ELSE { IF(score > 100) { bonus ← score
A., D.
3.8 Iteration Which of the following (2) algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ? A. Step 1:Set x to 0. Step 2:Increment x by 1. Step 3:If x is not divisible by 3, then display x. Step 4:Repeat steps 2 and 3 until x is 20. B. Step 1:Set x to 0. 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. C. Step 1:Set x to 1. 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. D. Step 1:Set x to 1. 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
A., D.
3.9 Developing Algorithms (flip card for grids) 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? A. Step 1:Keep moving forward, one square at a time, until the square to the right of the robot is black. Step 2:Turn right and move one square forward. Step 3:Repeat steps 1 and 2 three more times. B. Step 1:Keep moving forward, one square at a time, until the square to the right of the robot is no longer black. Step 2:Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times. C. Step 1:Move forward three squares. 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.
B.
3.6 Conditionals 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?
C.
3.6 Conditionals 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 during business hours. If it is, check if the gate sensor is activated. If it is, check if the gate is open. If it is not, turn on the motor.
3.3 Mathematical Expressions Which of the following code segments can be used to interchange the values of the variables num1 and num2 ?
Correct answer: D
3.8 Iteration 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. (robot on back of card) Which of the following code segments can be used to move the robot to the gray square? A. REPEAT 3 TIMES { MOVE_FORWARD() } REPEAT 2 TIMES { MOVE_FORWARD() } REPEAT 3 TIMES { MOVE_FORWARD() } B. REPEAT 8 TIMES { MOVE_FORWARD() } C. REPEAT 3 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 2 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 3 TIMES { MOVE_FORWARD() } D. REPEAT 3 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 2 TIMES { MOVE_FORWARD() } ROTATE_RIGHT() REPEAT 3 TIMES { MOVE_FORWARD() }
D.
3.7 Nested Conditionals 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 ? (flip for options)
II and III only
3.8 Iteration Suppose a large group of people in a room were all born in the same year. Consider the following three algorithms, which are each intended to identify the people in the room who have the earliest birthday based on just the month and day. For example, a person born on February 10 is considered to have an earlier birthday than a person born on March 5. Which of the three algorithms will identify the correct people? (options on back)
II only
3.6 Conditionals 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?
If num is less than the minimum, set the minimum equal to num. Otherwise, if num is greater than the maximum, set the maximum equal to num.
3.8 Iteration The following grid contains a robot represented as a triangle, which is initially facing right. (grid on other side of card) 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?
REPEAT 2 TIMES
3.3 Mathematical Expressions 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?
Step 1: Assign each student a unique integer from 1 to 30. 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.
3.3 Mathematical Expressions 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. (table on other side of card) Which of the following algorithms displays the correct distance for all possible values of num1 and num2 ?
Step 1: Subtract num1 from num2 and store the result in the variable diff. Step 2: Take the absolute value of diff and display the result.
3.7 Nested Conditionals Consider the following code segment with integer variables x and y. IF [x > 10] IF [y < 10] DISPLAY ["ONE"] ELSE DISPLAY ["TWO"] ELSE IF [y > 3] DISPLAY ["THREE"] ELSE DISPLAY ["FOUR"] 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?
THREE
3.7 Nested Conditionals 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 ?
When the value of exam is 80 and the value of presentation is 60
3.1 Variables and Assignments 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 ?
boolean
3.5 Boolean Expressions Consider the following code segment. a ← true b ← false c ← true a ← [NOT (a OR b) AND c] c ← [c AND a] DISPLAY a DISPLAY b DISPLAY c What is displayed as a result of executing the code segment?
false false false
3.1 Variables and Assignments 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 ?
number
3.3 Mathematical Expressions 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. (table on other side of card) Which of the following code segments can be used to update the values of the variables as shown in the table?
temp ← word1 word1 ← word3 word3 ← temp
3.5 Boolean Expressions 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?
true
3.8 Iteration Consider the following code segment. a ← true b ← false c ← true REPEAT UNTIL [a AND b] c ← NOT c b ← c DISPLAY [a] DISPLAY [b] DISPLAY [c] What is displayed as a result of executing the code segment?
true true true
3.5 Boolean Expressions 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?
val1 = false, val2 = false