5.5.1 Looping Unit Test

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

0

1

2

3

4

5

Answer: for i in range(6): print(i)

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

3

5

7

9

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

print(i)

Question: What is the value of num when this loop completes?

num = 0

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

num = num + i

Answer: 12

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

for i in range(10):

Answer: for i in range(0, 10):

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

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

Question: 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

Answer: It’s still a nice day

It’s still a nice day

It’s still a nice day

It’s still a nice day

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

Answer: while num < 12 or num < 3:

do something with num

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

Answer: secret_num = 10

while secret_num == 10:

print(secret_num)

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

Answer: When you don’t know how many times something will need to repeat

Question: 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)

Answer: 0

1

0

1

Donation Page

Support Our Work

Do you appreciate the value this website provides? If so, please consider donating to help keep it running. Your donation will go a long way in helping us continue to provide the same quality of content and services. Every bit helps, and your support is greatly appreciated. Thank you for your generosity.