Asked by john

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)


0
1
0
1

0
2

0
1
0
1
0
1

1
2
1
2
1
2
Question: 8
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


8


9


20


0

Question: 9
What does this program print?

for i in range(10):
if i == 3:
continue
print(i)


0
1
2

0
1
2
4
5
6
7
8
9

0
1
2
3
4
5
6
7
8
9

0
1
2
3
Question: 10
When would a while loop be a better control structure to use than a for loop?



When you need to repeat multiple commands


When you need to repeat something 5 times


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


When the user is inputting how many times to repeat something

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

0
1
2
3
4
5


for i in range(5):
print(i)

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

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

for i in range(0, 5, 1):
print(i)
Question: 12
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!")


The user can enter as many answers as they want


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


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


The loop will give the magic_number after a certain number of tries

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

num = 0

for i in range(2, 8, 2):
num = num + i


2


8


12


20

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


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


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


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: 15
Which of the following while loops will cause an infinite loop?



secret_num = 10

while secret_num == 10:
secret_num = secret_num - 1

secret_num = 10

while secret_num == 10:
print(secret_num)

secret_num = 10

while secret_num > 0:
secret_num = secret_num - 1

secret_num = 10

while secret_num != 10:
print(secret_num)

All Answers 1

Answered by GPT-5 mini AI
Initial program (the one before Question 8):
0
1
0
1

Question 8:
9

Question 9:
0
1
2
4
5
6
7
8
9

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

Question 11:
for i in range(6):
print(i)

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

Question 13:
12

Question 14:
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 15:
The second loop (the one with while secret_num == 10: print(secret_num)) causes an infinite loop.