NEW
Font size
WorksheetsChapter 8 Exam Review
Total questions: 20
Worksheet time: 10mins
What type of loop would you use if you want a numeric index and know how many times the loop should run?
"for" loop
"while" loop
infinite loop
"numeric" loop
When a "for" loop ends, where does the program flow continue?
At the first non-indented statement after the loop body
Program flow resumes at the statement directly above the "for" keyword
The entire program halts immediately when the loop is done
for() loops, by design, will never end
In the code, which statement(s) are part of the "for" loop body?
LINE 3 only
LINES 2 and 3 only
LINE 1 and 4 only
LINES 1, 2, and 3 only
How many times will the loop body run in the code below? data = "count" for x in data: print(x)
5
6
1
0
What value is stored in the "total" variable after the following code runs? total = 0 for i in range(1,4): total = total + i
6
0
5
10
What will be the output of the following "for" loop?
0 3 6 9 12 15 18 21 24 27
1 3 6 9 12 15 18 21 24 27 30
0 9 18 27
0 3 6 9
What will be displayed on the screen when the following code is run?
c#o#u#n#t
count
#t
#####
What kind of expression must be used as part of the "while" loop statement?
logical expression
mathematical expression
assignment expression
All of these will work
Which of the following best describes how many times the statements in the body of a "while" loop will run?
0, 1 or more times depending on the logical expression of the "while" statement
At least once
A maximum of 10 times no matter what
An infinite number of times until a "continue" statement is reached
How do you identify the statements that belong in the body of a "while" loop?
Indent them an equal number of spaces to the right of the "while" keyword
End each loop body statement with a colon (:)
All statements between the "while" and "endwhile" keywords are part of the loop body
Only the first statement after the "while" statement is ever part of the loop body
Which answer best describes the behavior of the following code?
The code will keep prompting the user for input until the user enters a string with 5 or more characters
The code will ask the user once for a string and print a success message if it is less than 5 characters long
The code will be stuck in an infinite loop, prompting the user for a new entry with no way out
The code will print "Success" when the user enters a secret password
What will be displayed when the following code is run?
0 2 4
1 2 3 4
0 2 4 6
2 4 6
How should programmers avoid creating an infinite "while" loop?
Make sure your loop body will eventually make the loop's logical expression False
Use a "for" loop instead of a "while" loop
Use the "continue" keyword as the last statement in the loop body
Add an "else" statement after the end of the loop
Given the following code, how many times will the "while" loop body execute?
Infinite loop
0
2
4
Carefully study the code below. What will be displayed when the program runs?
ABBB
ABBABBB
BAAA
BBBABBBA
Which of the following statements will cause a "while" loop to immediately exit without running any more statements in the body or checking the logical expression again?
break
continue
else
stop
Which answer best describes the purpose of the "continue" statement?
Immediately causes the next loop iteration to start, skipping any remaining statements in the loop
Immediately cause the loop to stop and program flow to continue after the loop body
Immediately causes the program to exit the entire program
Any of these can be true, depending on where "continue" is used
What will be displayed when the following code runs?
0 2 4
0
1 3 5
0 1 2 3 4
Given the code below, which answer best describes how many times the "while" loop body will run?
At least once, and maybe more depending on the user's answer to the input() question
None at all
Exactly one time
An infinite number of times, because the logical expression is always True
Given the code below, when will the user see "Surprise" printed to the screen?
When the user enters a string that contains no "a" characters
When the user enters a string that contains at least one "a" character
The user will always see "Surprise" no matter what is entered
The user will never see "Surprise"
