wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Chapter 8 Exam Review

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What type of loop would you use if you want a numeric index and know how many times the loop should run?

a)

"for" loop

b)

"while" loop

c)

infinite loop

d)

"numeric" loop

2.

When a "for" loop ends, where does the program flow continue?

a)

At the first non-indented statement after the loop body

b)

Program flow resumes at the statement directly above the "for" keyword

c)

The entire program halts immediately when the loop is done

d)

for() loops, by design, will never end

3.

In the code, which statement(s) are part of the "for" loop body?

a)

LINE 3 only

b)

LINES 2 and 3 only

c)

LINE 1 and 4 only

d)

LINES 1, 2, and 3 only

4.

How many times will the loop body run in the code below? data = "count" for x in data: print(x)

a)

5

b)

6

c)

1

d)

0

5.

What value is stored in the "total" variable after the following code runs? total = 0 for i in range(1,4): total = total + i

a)

6

b)

0

c)

5

d)

10

6.

What will be the output of the following "for" loop?

a)

0 3 6 9 12 15 18 21 24 27

b)

1 3 6 9 12 15 18 21 24 27 30

c)

0 9 18 27

d)

0 3 6 9

7.

What will be displayed on the screen when the following code is run?

a)

c#o#u#n#t

b)

count

c)

#t

d)

#####

8.

What kind of expression must be used as part of the "while" loop statement?

a)

logical expression

b)

mathematical expression

c)

assignment expression

d)

All of these will work

9.

Which of the following best describes how many times the statements in the body of a "while" loop will run?

a)

0, 1 or more times depending on the logical expression of the "while" statement

b)

At least once

c)

A maximum of 10 times no matter what

d)

An infinite number of times until a "continue" statement is reached

10.

How do you identify the statements that belong in the body of a "while" loop?

a)

Indent them an equal number of spaces to the right of the "while" keyword

b)

End each loop body statement with a colon (:)

c)

All statements between the "while" and "endwhile" keywords are part of the loop body

d)

Only the first statement after the "while" statement is ever part of the loop body

11.

Which answer best describes the behavior of the following code?

a)

The code will keep prompting the user for input until the user enters a string with 5 or more characters

b)

The code will ask the user once for a string and print a success message if it is less than 5 characters long

c)

The code will be stuck in an infinite loop, prompting the user for a new entry with no way out

d)

The code will print "Success" when the user enters a secret password

12.

What will be displayed when the following code is run?

a)

0 2 4

b)

1 2 3 4

c)

0 2 4 6

d)

2 4 6

13.

How should programmers avoid creating an infinite "while" loop?

a)

Make sure your loop body will eventually make the loop's logical expression False

b)

Use a "for" loop instead of a "while" loop

c)

Use the "continue" keyword as the last statement in the loop body

d)

Add an "else" statement after the end of the loop

14.

Given the following code, how many times will the "while" loop body execute?

a)

Infinite loop

b)

0

c)

2

d)

4

15.

Carefully study the code below. What will be displayed when the program runs?

a)

ABBB

b)

ABBABBB

c)

BAAA

d)

BBBABBBA

16.

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?

a)

break

b)

continue

c)

else

d)

stop

17.

Which answer best describes the purpose of the "continue" statement?

a)

Immediately causes the next loop iteration to start, skipping any remaining statements in the loop

b)

Immediately cause the loop to stop and program flow to continue after the loop body

c)

Immediately causes the program to exit the entire program

d)

Any of these can be true, depending on where "continue" is used

18.

What will be displayed when the following code runs?

a)

0 2 4

b)

0

c)

1 3 5

d)

0 1 2 3 4

19.

Given the code below, which answer best describes how many times the "while" loop body will run?

a)

At least once, and maybe more depending on the user's answer to the input() question

b)

None at all

c)

Exactly one time

d)

An infinite number of times, because the logical expression is always True

20.

Given the code below, when will the user see "Surprise" printed to the screen?

a)

When the user enters a string that contains no "a" characters

b)

When the user enters a string that contains at least one "a" character

c)

The user will always see "Surprise" no matter what is entered

d)

The user will never see "Surprise"