WorksheetsAPCS Principles Quarter 1 Review
Total questions: 56
Worksheet time: 30mins
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?
I. All the people in the room stand up. All standing people form pairs where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people sit down. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing. That person has the earliest birthday.
II. All the people in the room stand up. All standing people form pairs with another standing person that they have not previously been paired with where possible, leaving at most one person not part of a pair. For each pair, the person with the earlier birthday remains standing, while the other person in the pair sits down. If there is a tie, both people in the pair remain standing. Any individual not part of a pair remains standing. Continue doing this until only one person remains standing or all persons standing have the same birthday. Anyone still standing has the earliest birthday.
III. Beginning with the number 1, ask if anyone was born on that day of any month. Continue with the numbers 2, 3, and so on until a positive response is received. If only one person responds, that person has the earliest birthday. If more than one person responds, determine which person was born in the earliest month, and that person or those persons have the earliest birthday.
I only
II only
I and II only
II and III only
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 greater than the minimum, set the minimum equal to num. Otherwise, if num is greater 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 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 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 the maximum, set the maximum equal to num.
The figure 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?
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.
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.
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.
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 not black, repeat steps 1 and 2.
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 the middle number, swap them.
If the first number is greater than the middle number, swap them. Then, if the middle 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 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 than the last number, swap them. Then, if the first number is greater than the middle number, swap them.
Which of the following algorithms display all integers between 1 and 20, inclusive, that are not divisible by 3 ?
Select two answers.
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.
Step 1:
Set x to 0.
Step 2:
If x is divisible by 3, then display x.
Step 3:
Increment x by 1.
and 3 until x is greater than 20.
Step 4:
Repeat steps 2
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.
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 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 15.
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.
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.
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.
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.
A list of numbers has n elements, indexed from 1 to n. The following algorithm is intended to display the number of elements in the list that have a value greater than 100. The algorithm uses the variables count and position. Steps 3 and 4 are missing.
Step 1:
Set count to 0 and position to 1.
Step 2:
If the value of the element at index position is greater than 100, increase the value of count by 1.
Step 3(missing step)
Step 4(missing step)
Step 5:
Display the value of count.
Which of the following could be used to replace steps 3 and 4 so that the algorithm works as intended?
Step 3
Increase the value of position by 1.
Step 4
Repeat steps 2 and 3 until the value of count is greater than 100.
Step 3
Increase the value of position by 1.
Step 4
Repeat steps 2 and 3 until the value of position is greater than n.
Step 3
Repeat step 2 until the value of count is greater than 100.
Step 4
Increase the value of position by 1.
Step 3
Repeat step 2 until the value of position is greater than n.
Step 4
Increase the value of count by 1.
There are 32 students standing in a classroom. Two different algorithms are given for finding the average height of the students.
Algorithm A
Step 1: All students stand.
Step 2: A randomly selected student writes his or her height on a card and is seated.
Step 3: A randomly selected standing student adds his or her height to the value on the card, records the new value on the card, and is seated. The previous value on the card is erased.
Step 4: Repeat step 3 until no students remain standing.
Step 5: The sum on the card is divided by 32. The result is given to the teacher.
Algorithm B
Step 1: All students stand.
Step 2: Each student is given a card. Each student writes his or her height on the card.
Step 3: Standing students form random pairs at the same time. Each pair adds the numbers written on their cards and writes the result on one student’s card; the other student is seated. The previous value on the card is erased.
Step 4: Repeat step 3 until one student remains standing.
Step 5: The sum on the last student’s card is divided by 32. The result is given to the teacher.
Which of the following statements is true?
Algorithm A always calculates the correct average, but Algorithm B does not.
Algorithm B always calculates the correct average, but Algorithm A does not.
Both Algorithm A and Algorithm B always calculate the correct average.
Neither Algorithm A nor Algorithm B calculates the correct average.
What is displayed as a result of executing the algorithm in the flowchart?
5
1 5
1 2 3 4
1 2 3 4 5
Which of the following initial values of j and k will cause the algorithm represented in the flowchart to result in an infinite loop?
j = -5, k = 5
j = 0, k = 5
j = 5, k = 0
j = 5, k = -5
Based on the algorithm represented in the flowchart, what value is displayed if j has the initial value 3 and k has the initial value 4 ?
7
9
10
12
A school library allows students to borrow laptops. A computer program is used to count the number of times a particular laptop has been borrowed from the library and the number of times the same laptop has been returned to the library . Which of the following indicate that a particular laptop is not currently borrowed?
Select two answers.
The difference between and is zero.
The product of and is a positive even number.
The quotient when is divided by is greater than 1.The quotient when is divided by is greater than 1.
The sum of and is a positive even number.
If a game is begun by placing the game piece on the rightmost black space for step 1, what will be the value of the counter at the end of the game?
2
3
4
5
The following algorithm is intended to take a positive integer as input and display its individual digits in order from right to left. For example, if the input is 512, the algorithm should produce the output 2 1 5. Step 3 of the algorithm is missing.
Step 1:
Input a positive integer from the user and store it in the variable number.
Step 2:
Divide number by 10 and record the integer quotient and the remainder. The integer quotient is the quotient with any part after the decimal point dropped. For example, when 127 is divided by 10, the quotient is 12.7, the integer quotient is 12 and the remainder is 7.
Step 3: (missing step)
Step 4:
Repeat steps 2 and 3 until number is 0.
Which of the following can be used as step 3 so that the algorithm works as intended?
Step 3: Display the remainder of number divided by 10 and store the remainder in number.
Step 3: Display the remainder of number divided by 10 and store the integer quotient in number.
Step 3: Display the integer quotient of number divided by 10 and store the remainder in number.
Step 3: Display the integer quotient of number divided by 10 and store the integer quotient in number.
Which of the following code segments can be used to move the robot to the gray square?
Select two answers.
An algorithm has been developed to compute the sum of all the elements in a list of integers. Which of the following programming structures must be added to the existing algorithm so that the new algorithm computes the sum of only the even integers in the list?
Iteration
Searching
Selection
Sequencing
In Python, the following expression evaluates to what value?
7 % 4 + 5
0
2
6
8
In Python, the following expression evaluates to what value?
4/2**3
0
0.5
6
8
Select ALL valid variable names below.
hello
2hello
hello2
hello_2
hello-2
Which of the following lines of python code creates a variable called num and sets the value to 17?
17 = num
num: 17
num = 17
17 store in num
Which of the following lines of python code creates a variable called studentName and sets the value to “Maria”?
Maria = studentName
studentName = Maria
"Maria" = studentName
studentName = "Maria"
Which of the following lines of python code asks a user for their pet's name and stores it in a variable called petName?
petName = "What is your pet's name?"
petName = input("What is your pet's name?")
"What is your pet's name?" = petName
input("What is your pet's name?") = petName
Which of the following lines of python code asks a user for their grade point average and stores it in a variable called gpa. Note: gpa will need to be used in calculations later in the program so it needs to be read in as a number.
gpa = input(float("Enter gpa."))
gpa = float(input("Enter gpa."))
gpa = float("Enter gpa.")
gpa = input("Enter gpa.")
A programmer wants to ask the user for two legs of a right triangle and prints out the value of the hypotenuse. Below are several possible lines of code necessary to write this code.
A print(hypotenuse)
B x = input(“Enter leg #1.”)
C hypotenuse = math.sqrt(leg1**2 + leg2**2)
D y = float(input(“Enter leg #2.”))
E x = float(input(“Enter leg #1.”))
F y = input(“Enter leg #2”)
G import math
H hypotenuse = (leg1**2 + leg2**2) ^ (1/2)
G D E C A
G B F C A
G D E H A
G B F H A
What is the line of code that will generate a random integer between 1 and 50 inclusive
and store it in a variable called x?
x = random.random*50
x = random.random*51
x = random.randrange(1,50)
x = random.randrange(1,51)
Choose the line of code that will simulate rolling a six sided die and storing in a variable called die1.
die1 = random.randrange(0,6)
die1 = random.randrange(0,7)
die1 = random.randrange(1,6)
die1 = random.randrange(1,7)
Select TWO valid lines of code that calculate 45 and stores the answer in a variable called answer.
answer = 5**4
answer = 4**5
answer = math.pow(5,4)
answer = math.pow(4,5)
Select TWO valid lines of code that calculates the expression in the image and stores the answer in a variable called answer.
(17-x)**1/2
(17-x)**(1/2)
math.sqrt(17-x)
sqrt(17-x)
Given the formula in the image, if a, b and c have all been input by the user, which line of code will calculate the answer for this expression
and store it in a variable called result.
result = (a**3 + b**3)/3*c
result = (a**3 + b**3)/(3*c)
result = (a+b)**3/3*c
result = (a+b)**3/(3*c)
Select TWO FALSE statements about naming variables:
Names may contain spaces.
Names must start with a letter.
Names are case sensitive
Names may not contain numbers.
Given the assignment statement in the image, which of the following answers is true regarding the order that the operators will be applied based on Python order of operations?
% * / + -
* / % + -
+ % * / -
+ * % / -
What is printed as a result of running the following code segment?
x = 7
y = 3
x = y + 1
y = 2
print(“The answer is “ + str(x) + “ and “ + str(y))
The answer is 4 and 2
The answer is 4 and 4
The answer is 7 and 4
The answer is 7 and 3
What is printed as a result of running the following code segment?
a = 7
a = a * 2
print(“The answer is “ + str(a))
The answer is 14
The answer is 7
The answer is 2
There is a syntax error so nothing will be printed.
A program runs and, during running, is asked to divide by zero. The program crashes. This is an example of:
logic error
run-time error
syntax error
user error
What will the output be from the following code?
print(Hello world!)
Hello world!
Hello world
print(Hello world!)
SyntaxError
Which of the following statements best describes the output for this code?
The code will always output the correct answer.
The code will output the correct answer only when positive integers are entered for x and y.
The code will output an incorrect answer due to a logic error.
The code will not output any answer due to a syntax error.
Evaluate the following expression and determine if it is true or false.
10 >= 5 and 4 < 7
True
False
Evaluate the following expression and determine if it is true or false.
8 > 10 or 15 <= 15
True
False
Evaluate the following expression and determine if it is true or false.
20 % 4 != 1 and 6 / 3 > 1
True
False
Evaluate the following expression and determine if it is true or false.
7 2 < 5 or 2 * 5 == 25
True
False
Evaluate the following expression and determine if it is true or false.
not (9 / 3 != 7 and 7 % 2 == 1)
True
False
Hello
How are you?
Good Day!
Nothing will print.
Hello
How are you?
Good Day!
Nothing will print
Given a, b and c are all integer values that have been set by the user on previous lines of code that are not shown, what does the code segment print?
The code prints out all three integers.
The code prints out the smallest number stored in the three numbers.
The code prints out the largest number stored in the three numbers.
The code prints out the letters, “a” “b” and “c”.
A programmer wants to code the following scenario: Ask the user for two positive integer values, a and b. Check if both numbers are magic. To be magic, both numbers must be divisible by 3 and either their sum or difference must be divisible by 3. Print “True” if both numbers are divisible by 3, and if their sum or difference is divisible by 3. Otherwise print false. Which of the following code segments will correctly simulate this scenario?
Consider an algorithm that simulates two people picking a word out of a hat, and then counts the number of vowels in each word. The person with the greatest number of vowels is the winner. Display who is the winner or if there is a tie. Put the steps of the algorithm in order. Select TWO answers.
Step 1: Count and assign the number of vowels to the corresponding person.
Step 2: If person 2 chose a word with more vowels, display their name.
Step 3: Person 1 picks out a word from the hat.
Step 4: If the number of vowels in each word is the same, display "There is a tie!"
Step 5: If person 1 chose a word with more vowels, display their name.
Step 6: Person 2 picks out a word from the hat.
Select TWO answers.
Step 6, Step 3, Step 5, Step 2, Step 4, Step 1
Step 6, Step 3, Step 1, Step 5, Step 2, Step 4
Step 3, Step 6, Step 5, Step 1, Step 4, Step 2
Step 3, Step 6, Step 1, Step 4, Step 5, Step 2
A program needs to be written calculate sale prices at a retail store by asking the user for the total cost before tax. If the total cost before tax is over $50, save 20%. If the cost is over $30 but does not exceed $50. save 10%. Otherwise save 5%. Print out the dollar amount of the discount. Which of the following code segments will correctly simulate this scenario?
A program needs to be written to ask the user for the number of guests attending a birthday party and store that number in a variable. Ask the user for the number of tables available and store that number in a different variable. Assume each table can hold 8 guests. If there are not enough tables to seat all guests, determine how many extra chairs will be needed. If you have enough tables, print out a message that says "You don't need extra chairs". Otherwise print out a message informing the user how many extra chairs are needed. Which of the following code segments will correctly simulate this scenario?
A program needs to be written to ask the user for the temperature outside (in Fahrenheit). The program should print out the appropriate suggestions ("Don't go outside" if the temperature is 100 degrees or greater, "Stay cool" if 90 or greater, "Go out with sunscreen" if 80 or greater, "Take a long walk" if 70 or greater and "It's not hot today!" if below 70). Assume the temperature is a float input.
Choose TWO of the following code segments that will correctly simulate this scenario?
Consider the given code segment. What will be output if the user enters "1"?
Play a card game!
Watch a movie!
Eat a snack!
Play a card game!
Eat a snack!
Consider the given code segment. What will be output if the user enters "2"?
Play a card game!
Eat a snack!
Play a card game!
Eat a snack
Nothing will print out.
Consider the given code segment.
Which of the following values will print out "This integer is divisible by 2." ?
The value is not stored as a number, so the code will not execute properly.
11.5
23
44
Consider the given code segment, where exam and presentation are integer variables and grade is a string variable.
Under which of the following conditions will the value "No Credit" be assigned to the variable grade ?
When the value of exam is 3 and the value of passClass is False
When the value of exam is 2 and the value of passClass is True
When the value of exam is 2 and the value of passClass is False
When the value of exam is 5 and the value of passClass is True
A flowchart is a way to visually represent an algorithm. The following flow chart helps a student determine whether or not they should apply to a job posting. It considers whether the job is far from their home as well as the pay.
Which of the following statements is equivalent to the algorithm in the flowchart?
hours == "full time" or pay > minWage
hours == "full time" or pay >= minWage
hours == "full time" and pay >= minWage
hours == "full time" and pay > minWage
A family of 6 is taking a road trip and wants to make a stop within the next 10 miles at a fast food restaurant. The kids want a restaurant with a play ground and the parents want it to have outdoor tables. One of the kids makes a list of all the fast food restaurants within 10 miles. Online they find out the following information: if each restaurant has a playground and whether or not they have outdoor tables.
Which algorithm, if any, can the family use to find the number of fast food restaurants with play grounds and outdoor tables within 10 miles?
Algorithm I:
Set a counter equal to 0. Use the list of fast food restaurants within the next 10 miles. Set a counter equal to 0. Iterate through the list. Add 1 to the counter if the restaurant has a play ground and also has outdoor tables.
Algorithm II:
Filter the data by creating a new list of only fast food restaurants with outdoor tables. Set a counter equal to 0. Iterate through the list and add 1 to the counter for every restaurant that has a playground.
Algorithm III:
Filter the data by creating a new list of only fast food restaurants with playgrounds. Set a counter equal to 0. Iterate through the list and add 1 to the counter for every restaurant that has outdoor tables.
I only
II only
I and II only
I, II and III
A survey is given about the preferences of pies within the population of the U.S. The results were: 20% preferred peach, 50% preferred apple, and 30% preferred blueberry. If a random person is picked out, what is the code that depicts what that person prefers.
