wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Python - Chpt 4 - Review

Total questions: 25

Worksheet time: 13mins

Name
Class
Date
1.

What would be the output of the following code:

numbers = [1, 1, 2, 3]

for number in numbers:

if number % 2 == 0:

continue

print(number)

a)

1 1 2

b)

2

c)

1 1

d)

1 1 3

2.

What would be the output of the following code:

numbers = [2, 4, 6, 8]

for number in numbers:

print("hello!")

a)

hello!hello!hello!hello!

b)

hello!

c)

2 4 6 8

d)

2hello! 4hello! 6hello! 8hello!

3.

What would be the output of the following code:

for i in range(3):

print(5)

a)

5 5 5

b)

3 3 3 3 3

c)

0 1 2

d)

0 1 2 3 4

4.

What would be the output of the following code:

for i in range(3):

print(i)

a)

i i i i

b)

0 1 2

c)

i i i

d)

1 2 3

5.

What would be the output of the following code:

drink_choices = ["coffee", "tea", "water", "juice", "soda"] for for drink in drink_choices:

print(drink)

a)

"coffee"

b)

"coffee" "tea" "water" "juice" "soda"

c)

drink

d)

["coffee", "tea", "water", "juice", "soda"]

6.

What would be the output of the following code:

numbers = [1, 1, 2, 3]

for number in numbers:

if number % 2 == 0:

break

print(number)

a)

1 1 3

b)

2

c)

1 1

d)

1 1 2 3

7.

Fill in the blank with the appropriate while condition in order to print the numbers 1 through 10 in order:

i = 1

____________

print(i)

i += 1

a)

while i <= 10:

b)

while i > 10:

c)

while i < 10:

d)

while i == range(11)

8.

Which of the following for loops would print the following numbers?

0

1

2

3

4

5

a)

for i in range(5):

print(i)

b)

for i in range(1, 5, 1):

print(i)

c)

for i in range (6):

print(i)

d)

for i in range(0, 5, 1):

print(i)

9.

Which of the following for loops would print the following numbers?

3

5

7

9

a)

for i in range(3, 10, 2):

print(i)

b)

for i in range(3, 9, 2):

print(i)

c)

for i in range(9):

print(i)

d)

for i in range(3,9):

print(i)

10.

What is the value of num when this loop completes?

num = 0


for i in range(2, 8, 2):

num = num + i

a)

2

b)

8

c)

12

d)

20

11.

Which of the following for loops will give the same values for i as the loop below?

for i in range(10):

a)

for i in range(11,0):

b)

for i in range(10, 0, -1):

c)

for i in range(0, 10):

d)

for i in range(0, 11, 1):

12.

Three of the for loops below will provide identical values for i. Which for loop will provide values that do not match the others?

a)

for i in range(0, 5):

b)

for i in range(5):

c)

for i in range(0, 5, 1):

d)

for i in range(5, 0, 1):

13.

What does this program print?

temperature = 65


while temperature < 80:

print "It's still a nice day"

if temperature < 70:

temperature = temperature + 5

elif temperature > 75:

break

else:

temperature = temperature + 3

a)

It's still a nice day

It's still a nice day

b)

It's still a nice day

It's still a nice day

It's still a nice day

c)

It's still a nice day

It's still a nice day

It's still a nice day

It's still a nice day

d)

It's still a nice day

It's still a nice day

It's still a nice day

It's still a nice day

It's still a nice day

14.

Which of the following while loops would continue to loop as long as num has values in the range 3 to 12, exclusive?

a)

while num > 12 and num < 3:

# do something with num

b)

while num < 12 and num < 3:

# do something with num

c)

while num <= 12 and num >= 3:

# do something with num

d)

while num < 12 and num > 3:

# do something with num

15.

Which of the following while loops will cause an infinite loop?

a)

secret_num = 10


while secret_num == 10:

secret_num = secret_num - 1

b)

secret_num = 10


while secret_num == 10:

print(secret_num)

c)

secret_num = 10


while secret_num != 10:

print(secret_num)

d)

secret_num = 10


while secret_num > 0:

secret_num = secret_num - 1

16.

When would a while loop be a better control structure to use than a for loop?

a)

When you need to repeat multiple commands

b)

When you need to repeat something 5 times

c)

When you don't know how many times something will need to repeat

d)

When the user is inputting how many times to repeat something

17.

What will be printed to the screen when this program is run?

for j in range(2):

for i in range(0, 2, 1):

print i

a)

0

1

0

1

b)

0

2

c)

0

1

0

1

0

1

d)

1

2

1

2

1

2

18.

What is the value of sum when this loop completes?

sum = 0


for i in range(3):

sum = sum + 5

for j in range(2):

sum = sum - 1

a)

8

b)

9

c)

20

d)

0

19.

What will be printed to the screen when this program is run?

num = 100


while num > 0:

for i in range(100, 0, -25):

num = num - i

print num

a)

100

0

-75

-125

-150

b)

-150

c)

-175

d)

175

20.

What does this program print?

for i in range(10):

if i == 3:

continue

print i

a)

0

1

2

b)

0

1

2

4

5

6

7

8

9

c)

0

1

2

3

4

5

6

7

8

9

d)

0

1

2

3

21.

What is the benefit of using the break command as in the program below?

magic_number = 10


while True:

guess = int(input("Guess my number: "))

if guess == magic_number:

print "You got it!"

break

print "Try again!"

a)

The user can enter as many answers as they want

b)

The program will break out of the loop as soon as the user enters the magic_number

c)

The loop will prompt the user to enter a number no matter what input they give

d)

The loop will give the magic_number after a certain of tries

22.

for loops ______, meaning it marches through the sequence one element at a time.

a)

sequencing

b)

iterate

c)

index

d)

slice

23.

If the user enters ‘4’, I want the loop to exit. Where should the break command be placed in order to end the loop when the user enters this value?

a)

On line 3

b)

On line 5

c)

On line 7

d)

On line 9

24.

The _____ command allows you to stop a for loop from inside the loop.

a)

break

b)

pass

c)

continue

d)

skip

25.

Specifying a position (or index) number in a sequence and then getting the element at that position is called ____.

a)

sequencing

b)

accessing

c)

slicing

d)

indexing