Font size
WorksheetsAPCSP AP Test Review
Total questions: 40
Worksheet time: 32mins
An algorithm will be used to calculate the difference between the smallest and largest values in a list. For the list of [10, 3, 5, 6], it should calculate a difference of 7.
There are two proposals for the algorithm:
Algorithm 1: Set minVal to the first value in the list and maxVal to the last value in the list. Iterate through each number in the list. If the number is greater than maxVal, store it in maxVal. If the number is less than minVal, store it in minVal. After loop, set maxDiff to the difference between maxVal and minVal.
Algorithm 2: Set minVal to 1000 and maxVal to 0. Iterate through each number in the list. If the number is greater than maxVal, store it in maxVal. If the number is less than minVal, store it in minVal. After loop, set maxDiff to the difference between maxVal and minVal.
Which of these statements are true about these algorithms?
I. Algorithm 1 does not work on lists where the smallest value is at the start of the list or the largest value is at the end of the list.
II. Algorithm 2 does not work on lists that contain all negative numbers or all numbers over 1000.
Choose 1 answer:
Choose 1 answer:
I
II
I and II
Neither I or II
"Aba speak" is a game invented by school children to disguise what they're speaking.
When speaking, they transform each word following this algorithm:
For each letter in word:
If letter is vowel:
Add "b" after
Add that letter after "b"
Following that algorithm, how would they transform the word "avocado"?
Choose 1 answer:
Choose 1 answer:
abvobcabdob
avabocabadabo
abavobocabadobo
abavbvobocbcabadbdobo
abavobacabadoba
Determine what is printed by the following code.
values ⬅ [5, 6, 7, 0, 3, 3]
baz ⬅ 0
FOR EACH value IN values
{
if( value ≥ 7 )
{
baz ⬅ baz + 1
}
}
DISPLAY( baz )
1
0
Determine what is printed by the following code.
list ⬅ [11, 2, 8, 6, 4, 2, 5, 6, 9, 12]
fizz ⬅ 0
thud ⬅ 0
FOR EACH item IN list
{
if( item MOD 3 = 2 )
{
thud ⬅ thud + 1
}
fizz ⬅ fizz + 1
}
DISPLAY( thud / fizz )
0
1
0.5
5/10
A programmer for a weather website needs to display the proportion of days with freezing temperatures in a given month.
Their algorithm will operate on a list of temperatures for each day in the month. It must keep track of how many temperatures are below or equal to 32. Once it's done processing the list, it must display the ratio of freezing days over total days.
Which of these correctly expresses that algorithm in pseudocode?
numFreezing ← 0
numDays ← 0
FOR EACH temp IN temps {
IF (temp ≤ 32) {
numFreezing ← numFreezing + 1
}
numDays ← numDays + 1
DISPLAY(numFreezing/numDays)
}
numFreezing ← 0
numDays ← 0
FOR EACH temp IN temps {
IF (temp ≤ 32) {
numFreezing ← numFreezing + 1
}
numDays ← numDays + 1
}
DISPLAY(numFreezing/numDays)
numFreezing ← 0
numDays ← 0
FOR EACH temp IN temps {
IF (temp < 32) {
numFreezing ← numFreezing + 1
}
numDays ← numDays + 1
}
DISPLAY(numFreezing/numDays)
numFreezing ← 0
numDays ← 0
FOR EACH temp IN temps {
IF (temp ≤ 32) {
numFreezing ← numFreezing + 1
numDays ← numDays + 1
}
}
DISPLAY(numFreezing/numDays)
A programmer is deciding between using a linear or binary search to find a target value in a sorted list. Which of the following is true?
In all cases, a binary search of a sorted list requires fewer comparisons than a linear search.
Generally, the advantage of using a binary search over a linear search increases as the size of the list increases.
A linear search will generally run faster than a binary search because a linear search requires fewer lines of code to implement.
Using a linear search is preferable to using a binary search if there is a chance that the target may not be found in the list.
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
An algorithm will be used to identify the maximum value in a list of one or more integers. Consider the two versions of the algorithm below.
Algorithm I : Set the value of a variable max to − 1. Iterate through the list of integer values. If a data value is greater than the value of the variable max, set max to the data value.
Algorithm II : Set the value of a variable max to the first data value. Iterate through the remaining values in the list of integers. If a data value is greater than the value of the variable max, set max to the data value.
Which of the following statements best describes the behavior of the two algorithms?
Both algorithms work correctly on all input values.
Algorithm I always works correctly, but Algorithm II only works correctly when the maximum value is not the first value in the list.
Algorithm II always works correctly, but Algorithm I only works correctly when the maximum value is greater than or equal to − 1.
Neither algorithm will correctly identify the maximum value when the input contains both positive and negative input values.
Central High School keeps a database of information about each student, including the numeric variables numberOfAbsences and gradePointAverage. The expression below is used to determine whether a student is eligible to receive an academic award.
(numberOfAbsences ≤ 5) AND (gradePointAverage > 3.5)
Which of the following pairs of values indicates that a student is eligible to receive an academic award?
numberOfAbsences = 3, gradePointAverage = 3.5
numberOfAbsences = 5, gradePointAverage = 3.8
numberOfAbsences = 6, gradePointAverage = 3.4
numberOfAbsences = 6, gradePointAverage = 3.6
Consider the following code segment, which uses the variables r, s, and t.
r ← 1
s ← 2
t ← 3
r ← s
s ← t
DISPLAY (r)
DISPLAY (s)
11
12
23
32
A certain computer game is played between a human player and a computer-controlled player. Every time the computer-controlled player has a turn, the game runs slowly because the computer evaluates all potential moves and selects the best one.
Which of the following best describes the possibility of improving the running speed of the game?
The game’s running speed can only be improved if the game is played between two human players instead of with the computer-controlled player.
The game’s running speed might be improved by using a process that finds approximate solutions every time the computer-controlled player has a turn.
The game’s running speed cannot be improved because computers can only be programmed to find the best possible solution.
The game’s running speed cannot be improved because the game is an example of an algorithm that does not run in a reasonable time
A programmer completes the user manual for a video game she has developed and realizes she has reversed the roles of goats and sheep throughout the text.
Consider the programmer’s goal of changing all occurrences of “goats” to “sheep” and all occurrences of “sheep” to “goats.” The programmer will use the fact that the word “foxes” does not appear anywhere in the original text.
Which of the following algorithms can be used to accomplish the programmer’s goal?
First, change all occurrences of “goats” to “sheep.”
Then, change all occurrences of “sheep” to “goats.”
First, change all occurrences of “goats” to “sheep.”
Then, change all occurrences of “sheep” to “goats.”
Last, change all occurrences of “foxes” to “sheep.”
First, change all occurrences of “goats” to “foxes.”
Then, change all occurrences of “sheep” to “goats.”
Last, change all occurrences of “foxes” to “sheep.”
First, change all occurrences of “goats” to “foxes.”
Then, change all occurrences of “foxes” to “sheep.”
Last, change all occurrences of “sheep” to “goats.”
Determine what is printed by the following code.
numbers ⬅ [7, 4, 1, 6, 4, 11, 3]
baz ⬅ 0
FOR EACH value IN numbers
{
if( value < 3 )
{
baz ⬅ baz + 1
}
}
DISPLAY( baz )
0
1
none of the above
Determine what is printed by the following code.
numbers ⬅ [8, 5, 7, 1, 12, 12]
fred ⬅ 0
FOR EACH number IN numbers
{
if( number MOD 2 = 0 )
{
fred ⬅ fred + 1
}
}
DISPLAY( fred )
3
2
1
0
for (var i = 10; i > 5; i--){
console.log ("p"); }
How many times will "p" be printed?
10
5
6
infinite loop
Based on the following, determine whether each expression is TRUE or FALSE
var score= 80;
(score != 90) && (score > 70)
True
False
What 2 items below make your code easier to read and manage/edit later?
good variable names
testing your code often
write perfect code the first time
comments
Determine what is printed by the following code.
values ⬅ [5, 6, 7, 0, 3, 3]
baz ⬅ 0
FOR EACH value IN values
{
if( value ≥ 7 )
{
baz ⬅ baz + 1
}
}
DISPLAY( baz )
1
0
Consider the following code segment, which uses the variables r, s, and t.
r ← 1
s ← 2
t ← 3
r ← s
s ← t
DISPLAY (r)
DISPLAY (s)
11
12
23
32
Determine what is printed by the following code.
numbers ⬅ [7, 4, 1, 6, 4, 11, 3]
baz ⬅ 0
FOR EACH value IN numbers
{
if( value < 3 )
{
baz ⬅ baz + 1
}
}
DISPLAY( baz )
0
1
none of the above
One data point that is stored in a list. It is a single part of a larger group.
Element
Index
List
A data structure that stores one or more similar types of values in a single value. A collection of individual values that are related.
Element
Index
List
Identifies a value’s unique position on a list. In SNAP & on the APCSP exam the first item is stored at index 1.
Element
Index
List
Performing the same operation on each item of the list, sequentially.
Traverse
Remove/Delete
Append
What is displayed from this code?
1,2,3,4,5
Nothing, it won't run
1, 2, 4, 8
1, 2, 4
A student wants to create an algorithm that can determine, given any program and program input, whether or not the program will go into an infinite loop for that input. The problem the student is attempting to solve is considered an undecidable problem. Which of the following is true?
It is possible to create an algorithm that will solve the problem for all programs and inputs, but the
algorithm can only be implemented in a low-level programming language.
It is possible to create an algorithm that will solve the problem for all programs and inputs, but the
algorithm requires simultaneous execution on multiple CPUs.
It is possible to create an algorithm that will solve the problem for all programs and inputs, but the
algorithm will not run in reasonable time.
It is not possible to create an algorithm that will solve the problem for all programs and inputs.
An algorithm with an efficiency rating of
would be considered
reasonable time
unreasonable time
The action of doing something over and over again.
Looping
Bugging
Programming
If the length of the list is 100 times longer, the search will probably on average take 100 times longer to complete.
This is true using a:
linear search
binary search
If the length of the list is 100 times longer, the search will might on average only take 7-8 times longer to complete.
This is true using a:
linear search
binary search
Hex = ?
A unit of data that is eight binary digits long. Bytes are often used to represent a character such as a letter, number, space
Bit
Byte
Block
