Iteration Quiz
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.
REPEAT 2 TIMES
Which of the following code segments can be used to move the robot to the gray square?
REPEAT 3 TIMES { MOVE_FORWARD() } ROTATE_LEFT() REPEAT 2 TIMES { MOVE_FORWARD() } ROTATE_RIGHT() REPEAT 3 TIMES { MOVE_FORWARD() }
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?
14
Consider the following code segment. What is the value of result after the code segment is executed?
15
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 { sum ← sum + 1 } sum ← 0 REPEAT y TIMES { REPEAT x TIMES { sum ← sum + 1 } }
Consider the following code segment. What is displayed as a result of executing the code segment?
true true true
Consider the following code segment. What is displayed as a result of executing the code segment?
100 300 500
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?
A) LENGTH(scores) - 1
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, 2 * i) i ← i + 1
Which of the following should replace <MISSING CODE> so that the code segment works as intended?
FOR EACH vote IN voteList
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?
IF(num < 0) { DISPLAY("negative") } ELSE { IF(num = 0) { DISPLAY("zero") } ELSE { DISPLAY("positive") } }
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?
II only
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 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.
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?
Inserting the statement count ← count + 1 between line 6 and line 7
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?
No change is needed; the algorithm is correct as is.
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 no longer black. Step 2: Turn right and move one square forward. Step 3: Repeat steps 1 and 2 three more times.
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 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.
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?
count1 = 2, count2 = 3