Skip to content

Module 7 Chapter 5 Answer

Question 1

What is wrong with this Python loop?

n = 5
while n > 0 :
print(n)
print('All done')

A. This loop will run forever
B. The print('All done') statement should be indented four spaces
C. There should be no colon on the while statement
D. while is not a Python reserved word

Correct Answer: B. The print('All done') statement should be indented four spaces

🔹 Explanation:
In Python, statements inside a loop should be properly indented. Since print('All done') is not indented, it is outside the loop and will execute only once after the loop completes.


Question 2

What does the break statement do?

A. Resets the iteration variable to its initial value
B. Jumps to the “top” of the loop and starts the next iteration
C. Exits the currently executing loop
D. Exits the program

Correct Answer: C. Exits the currently executing loop

🔹 Explanation:
The break statement immediately terminates the enclosing loop and continues execution after the loop.


Question 3

What does the continue statement do?

A. Jumps to the “top” of the loop and starts the next iteration
B. Resets the iteration variable to its initial value
C. Exits the program
D. Exits the currently executing loop

Correct Answer: A. Jumps to the “top” of the loop and starts the next iteration

🔹 Explanation:
The continue statement skips the remaining code inside the loop for the current iteration and jumps to the next iteration.


Question 4

What does the following Python program print out?

tot = 0
for i in [5, 4, 3, 2, 1]:
tot = tot + 1
print(tot)

A. 0
B. 5
C. 10
D. 15

Correct Answer: B. 5

🔹 Explanation:
The loop iterates 5 times, incrementing tot by 1 each time. So, tot = 5 after the loop.


Question 5

What is the iteration variable in the following Python code?

friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends:
print('Happy New Year:', friend)
print('Done!')

A. friend
B. Sally
C. Glenn
D. Joseph

Correct Answer: A. friend

🔹 Explanation:
In a for loop, the iteration variable (loop variable) takes on each value in the list one by one. Here, friend is the iteration variable.


Question 6

What is a good description of the following bit of Python code?

zork = 0
for thing in [9, 41, 12, 3, 74, 15]:
zork = zork + thing
print('After', zork)

A. Count all of the elements in a list
B. Sum all the elements of a list
C. Find the largest item in a list
D. Find the smallest item in a list

Correct Answer: B. Sum all the elements of a list

🔹 Explanation:
The loop iterates through the list, adding each element to zork. This results in the sum of all elements.


Question 7

What will the following code print out?

smallest_so_far = -1
for the_num in [9, 41, 12, 3, 74, 15]:
if the_num < smallest_so_far:
smallest_so_far = the_num
print(smallest_so_far)

🔹 Hint: This is a trick question and most would say this code has a bug – so read carefully.

A. -1
B. 74
C. 42
D. 3

Correct Answer: A. -1

🔹 Explanation:
The if condition checks if the_num is smaller than smallest_so_far (-1). Since all numbers in the list are greater than -1, smallest_so_far is never updated.


Question 8

What is a good statement to describe the is operator as used in the following if statement?

if smallest is None:
smallest = value

A. Is true if the smallest variable has a value of -1
B. Matches both type and value
C. Looks up 'None' in the smallest variable if it is a string
D. The if statement is a syntax error

Correct Answer: B. Matches both type and value

🔹 Explanation:
The is operator checks whether two variables point to the same object in memory. In this case, it checks whether smallest is exactly None.


Question 9

Which reserved word indicates the start of an “indefinite” loop in Python?

A. def
B. for
C. while
D. break
E. indef

Correct Answer: C. while

🔹 Explanation:
A while loop runs indefinitely until the condition becomes false, making it an “indefinite” loop.


Question 10

How many times will the body of the following loop be executed?

n = 0
while n > 0:
print('Lather')
print('Rinse')
print('Dry off!')

A. 5
B. 0
C. This is an infinite loop
D. 1

Correct Answer: B. 0

🔹 Explanation:
Since n = 0, the condition n > 0 is false from the beginning, so the loop body never executes.