SG Algorithms
SG Algorithms
SG Algorithms
Algorithms 01
1. Which of the following best explains the ability to solve problems algorithmically?
Any problem can be solved algorithmically, though some algorithmic solutions may require humans to
(A)
validate the results.
Any problem can be solved algorithmically, though some algorithmic solutions must be executed on
(B)
multiple devices in parallel.
Any problem can be solved algorithmically, though some algorithmic solutions require a very large
(C)
amount of data storage to execute.
(D) There exist some problems that cannot be solved algorithmically using any computer.
2. 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.
3. 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.
4. Under which of the following conditions is it most beneficial to use a heuristic approach to solve a problem?
(A) When the problem can be solved in a reasonable time and an approximate solution is acceptable
(B) When the problem can be solved in a reasonable time and an exact solution is needed
(C) When the problem cannot be solved in a reasonable time and an approximate solution is acceptable
(D) When the problem cannot be solved in a reasonable time and an exact solution is needed
Algorithms 01
5. 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?
Algorithms 01
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
Step 2:
Turn right and move one square forward.
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.
Step 2:
(D) 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.
Algorithms 01
6. The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the
center square and facing toward the top of the grid.
The following code segment is used to move the robot in the grid.
count 1
REPEAT 4 TIMES
{
REPEAT count TIMES
{
MOVE_FORWARD()
}
ROTATE_LEFT()
count ← count + 1
}
Which of the following code segments will move the robot from the center square along the same path as the code
segment above?
Algorithms 01
count 0
REPEAT 4 TIMES
{
count ← count + 1
REPEAT count TIMES
(A) {
MOVE_FORWARD()
}
ROTATE_LEFT()
}
count 0
REPEAT 4 TIMES
{
count ← count + 1
ROTATE_LEFT()
(B) REPEAT count TIMES
{
MOVE_FORWARD()
}
}
count 0
REPEAT 4 TIMES
{
REPEAT count TIMES
{
(C) ROTATE_LEFT()
}
MOVE_FORWARD()
count ← count + 1
}
count 0
REPEAT 4 TIMES
{
ROTATE_LEFT()
REPEAT count TIMES
(D) {
MOVE_FORWARD()
}
count ← count + 1
}
Algorithms 01
7. 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?
Algorithms 01
8. An online game collects data about each player’s performance in the game. A program is used to analyze the data to
make predictions about how players will perform in a new version of the game.
The procedure GetPrediction (idNum) returns a predicted score for the player with ID number idNum.
Assume that all predicted scores are positive. The GetPrediction procedure takes approximately 1 minute to
return a result. All other operations happen nearly instantaneously.
Version I
topScore 0
idList [1298702, 1356846, 8848491, 8675309]
FOR EACH id IN idList
{
score ← GetPrediction (id)
IF (score > topScore)
{
topScore ← score
}
}
DISPLAY (topScore)
Version II
Which of the following best compares the execution times of the two versions of the program?
(A) Version I requires approximately 1 more minute to execute than version II.
(B) Version I requires approximately 5 more minutes to execute than version II.
(C) Version II requires approximately 1 more minute to execute than version I.
(D) Version II requires approximately 5 more minutes to execute than version I.
Algorithms 01
Which of the following best compares the values displayed by programs A and B?
(A) Program A and program B display identical values in the same order.
(B) Program A and program B display the same values in different orders.
(C) Program A and program B display the same number of values, but the values differ.
(D) Program B displays one more value than program A.
10. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
Which of the following changes will NOT affect the results when the code segment is executed?
Algorithms 01
11. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
The two code segments below are each intended to display the average of the numbers in the list
. Assume that contains more than one value.
Both code segments display the correct average, but code segment I requires more arithmetic operations
(C)
than code segment II.
Both code segments display the correct average, but code segment II requires more arithmetic operations
(D)
than code segment I.
Algorithms 01
12. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
Which of the following best compares the values displayed by programs A and B?
(A) Program A and program B display identical values.
(B) Program A and program B display the same values in different orders.
(C) Program A and program B display the same number of values, but the values differ.
Algorithms 01
13. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the
bottom right square of the grid and facing toward the top of the grid.
The following programs are each intended to move the robot to the gray square. Program II uses the procedure
, which returns if the robot is in the gray square and returns
otherwise.
(C) Both program I and program II correctly move the robot to the gray square.
(D) Neither program I nor program II correctly moves the robot to the gray square.
Algorithms 01
14. Programs I and II below are each intended to calculate the sum of the integers from 1 to n. Assume that n is a
positive integer (e.g., 1, 2, 3, …).
Which of the following best describes the behavior of the two programs?
(A) Program I displays the correct sum, but program II does not.
(B) Program II displays the correct sum, but program I does not.
(C) Both program I and program II display the correct sum.
(D) Neither program I nor program II displays the correct sum.
15. 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.
16. 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?
Algorithms 01
The game’s running speed can only be improved if the game is played between two human players
(A)
instead of with the computer-controlled player.
The game’s running speed might be improved by using a process that finds approximate solutions every
(B)
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
(C)
best possible solution.
The game’s running speed cannot be improved because the game is an example of an algorithm that
(D)
does not run in a reasonable time.
17. 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.
List Size
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?
Algorithms 01
18. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
A video-streaming service maintains a database of information about its customers and the videos they have
watched.
The program below analyzes the data in the database and compares the number of viewers of science fiction videos
to the number of viewers of videos of other genres. It uses the procedure , which
returns the number of unique users who viewed videos of a given category in the past year. The
procedure takes approximately 1 hour to return a result, regardless of the number of videos of the given genre. All
other operations happen nearly instantaneously.
Which of the following best approximates the amount of time it takes the program to execute?
(A) 1 hour
(B) 2 hours
(C) 4 hours
(D) 5 hours
19. 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 ?
Algorithms 01
(A)
(B)
(C)
(D)
Algorithms 01
20. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
An online retailer uses an algorithm to sort a list of n items by price. The table below shows the approximate
number of steps the algorithm takes to sort lists of different sizes.
Based on the values in the table, which of the following best characterizes the algorithm for very large values of n ?
Algorithms 01
21. A programmer wrote the code segment below to display the average of all the elements in a list called numbers.
There is always at least one number in the list.
The programmer wants to reduce the number of operations that are performed when the program is run. Which
change will result in a correct program with a reduced number of operations performed?
(A) Interchanging line 1 and line 2
(B) Interchanging line 5 and line 6
(C) Interchanging line 6 and line 7
(D) Interchanging line 7 and line 8
22. Consider the following algorithms. Each algorithm operates on a list containing n elements, where n is a very large
integer.
Algorithms 01
Every problem can be solved with an algorithm for all possible inputs, in a reasonable amount of time,
(A)
using a modern computer.
Every problem can be solved with an algorithm for all possible inputs, but some will take more than 100
(B)
years, even with the fastest possible computer.
Every problem can be solved with an algorithm for all possible inputs, but some of these algorithms
(C)
have not been discovered yet.
(D) There exist problems that no algorithm will ever be able to solve for all possible inputs.
Algorithms 01
24. The question below uses a robot in a grid of squares. The robot is represented as a triangle, which is initially in the
bottom left square of the grid and facing right.
The following programs are each intended to move the robot to the gray square. Program II uses the procedure
GoalReached, which returns true if the robot is in the gray square and returns false otherwise.
Algorithms 01
Which of the following statements best describes the correctness of the programs?
(A) Program I correctly moves the robot to the gray square, but program II does not.
(B) Program II correctly moves the robot to the gray square, but program I does not.
(C) Both program I and program II correctly move the robot to the gray square.
(D) Neither program I nor program II correctly moves the robot to the gray square.
25. 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.
26. 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
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?
Algorithms 01
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.
27. A company delivers packages by truck and would like to minimize the length of the route that each driver must
travel in order to reach delivery locations. The company is considering two different algorithms for determining
delivery routes.
Algorithm I Generate all possible routes, compute their lengths, and then select the shortest possible
route. This algorithm does not run in reasonable time.
Algorithm II Starting from an arbitrary delivery location, find the nearest unvisited delivery location.
Continue creating the route by selecting the nearest unvisited location until all locations
have been visited. This algorithm does not guarantee the shortest possible route and runs
in time proportional to .
Algorithms 01
28. A flowchart is a way to visually represent an algorithm. The flowchart below is used by an application to set the
Boolean variable available to true under certain conditions. The flowchart uses the Boolean variable
weekday and the integer variable miles.
Block Explanation
Oval The start or end of the algorithm
A conditional or decision step, where execution proceeds to the side labeled true if the
Diamond
condition is true and to the side labeled false otherwise
Rectangle One or more processing steps, such as a statement that assigns a value to a variable
Algorithms 01
(A)
(B)
(C)
(D)
29. 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.
30. 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?
Algorithms 01
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")
}
Algorithms 01
31. Which of the following best explains why it is not possible to use computers to solve every problem?
(A) Current computer processing capabilities cannot improve significantly.
Large-scale problems require a crowdsourcing model, which is limited by the number of people
(B)
available to work on the problem.
The ability of a computer to solve a problem is limited by the bandwidth of the computer’s Internet
(C)
connection.
(D) There exist some problems that cannot be solved using any algorithm.
32. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
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
(A)
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
(B)
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
(C)
algorithm will not run in reasonable time.
(D) It is not possible to create an algorithm that will solve the problem for all programs and inputs.
Algorithms 01
33. 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.
Algorithms 01
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.
Algorithms 01
Which of the following expressions represents the value stored in the variable x as a result of executing the
program?
(A) 2 * 3 * 3 * 3
(B) 2 * 4 * 4 * 4
(C) 2 * 3 * 3 * 3 * 3
(D) 2 * 4 * 4 * 4 * 4
Algorithms 01
35. Directions: The question or incomplete statement below is followed by four suggested answers or
completions. Select the one that is best in each case.
A student is creating a procedure to determine whether the weather for a particular month was considered very hot.
The procedure takes as input a list containing daily high temperatures for a particular month. The procedure is
intended to return if the daily high temperature was at least 90 degrees for a majority of days in the month
and return otherwise.
Which of the following can be used to replace so that the procedure works as intended?
(A)
(B)
(C)
(D)
Algorithms 01
36. 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?
Algorithms 01
(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
37. Which of the following programs is most likely to benefit from the use of a heuristic?
(A) A program that calculates a student’s grade based on the student’s quiz and homework scores
(B) A program that encrypts a folder of digital files
(C) A program that finds the shortest driving route between two locations on a map
(D) A program that sorts a list of numbers in order from least to greatest