Quiz: Python Loops and Lists
1. A "for" loop is typically used when the number of loop cycles is known.
A. True
- Correct. Use a "for" loop to cycle through a specified number of times.
B. False
- Incorrect. Use a "while" loop when the number of cycles is based on a condition, not number of cycles.
2. To skip an individual cycle of a for loop, the following command is used:
A. break
- Incorrect. break terminates the loop
B. continue
- Correct. continue initiates the next cycle of the loop
C. cycle
- Incorrect. Use a "continue" statement
D. skip
- Incorrect. Use a "continue" statement
3. What will the following code print as a result for sum?
sum = 0
for i in range(5):
if i==3:
continue
sum = sum + i
print(sum)
for i in range(5):
if i==3:
continue
sum = sum + i
print(sum)
A. 0
- Incorrect. Skip the 3 for 0+1+2+4 = 7
B. 3
- Incorrect. Skip the 3 for 0+1+2+4 = 7
C. 7
- Correct. Skip the 3 for 0+1+2+4 = 7
D. 10
- Incorrect. Skip the 3 for 0+1+2+4 = 7