Welcome to the Python Loops Quiz!
Loops are a fundamental concept in Python that help automate repetitive tasks.
This quiz will test your understanding of for
loops, while
loops, loop control statements, and iteration techniques. Get ready to loop through these questions and test your skills!
Table of Contents:
- Python quizzes for beginners series
Loops Quiz
Quiz Questions
1. What is the main purpose of a loop in Python?
a) To repeat a block of code multiple times
b) To define a function
c) To create a variable
d) To handle exceptions
Answer
a) To repeat a block of code multiple times
2. Which of the following is a valid for
loop in Python?
a) for i = 1 to 5:
b) for i in range(5):
c) loop(i, 5):
d) for(i=0; i<5; i++)
Answer
b) for i in range(5):
3. What will be the output of this loop?
for i in range(3):
print("Hello")
a) Prints “Hello” 3 times
b) Prints “HelloHelloHello”
c) Syntax error
d) Infinite loop
Answer
a) Prints “Hello” 3 times
4. How many times will the following while
loop execute?
count = 0
while count < 5:
print(count)
count += 1
a) 4
b) 5
c) Infinite times
d) 6
Answer
b) 5
5. What keyword is used to exit a loop prematurely?
a) exit
b) break
c) stop
d) continue
Answer
b) break
6. What does the continue
statement do in a loop?
a) Stops the loop execution
b) Skips the current iteration and moves to the next
c) Repeats the current iteration
d) None of the above
Answer
b) Skips the current iteration and moves to the next
7. What will be the output of this loop?
for i in range(3, 8, 2):
print(i)
a) 3 5 7
b) 3 4 5 6 7
c) 3 5
d) 3 5 7 9
Answer
a) 3 5 7
8. Which loop is guaranteed to execute at least once?
a) for
loop
b) while
loop
c) do-while
loop
d) None of the above
Answer
c) do-while
loop (Python does not have a do-while
loop, but in other languages, it runs at least once)
9. What will happen if the loop condition never becomes False
?
a) The loop will execute once
b) The loop will run infinitely
c) Python will throw an error
d) The loop will skip
Answer
b) The loop will run infinitely
10. What will this code output?
for i in range(2):
for j in range(2):
print(i, j)
a) (0,0) (0,1) (1,0) (1,1)
b) (0,0) (1,1)
c) (0,1) (1,0)
d) None of the above
Answer
a) (0,0) (0,1) (1,0) (1,1)
11. How do you create an infinite loop?
a) while True:
b) for i in range(inf):
c) while i != 100:
d) for i in range(0, -1):
Answer
a) while True:
12. What is the output of this loop?
for i in range(5, 0, -1):
print(i)
a) 0 1 2 3 4 5
b) 5 4 3 2 1
c) 1 2 3 4 5
d) None of the above
Answer
b) 5 4 3 2 1
13. Which of the following is NOT a valid loop control statement?
a) break
b) continue
c) pass
d) skip
Answer
d) skip
14. What will be the output?
for i in range(3):
print(i)
else:
print("Done")
a) 0 1 2 Done
b) 0 1 2
c) Done
d) Error
Answer
a) 0 1 2 Done
15. Which function generates a sequence of numbers in loops?
a) list()
b) range()
c) sequence()
d) numbers()
Answer
b) range()
16. What does the enumerate()
function do in a loop?
a) Counts iterations
b) Returns index and value of an iterable
c) Prints numbers
d) Stops the loop
Answer
b) Returns index and value of an iterable
17. What does the following loop do?
for _ in range(3):
print("Python")
a) Prints “Python” 3 times
b) Prints an error
c) Prints “Python” once
d) None of the above
Answer
a) Prints “Python” 3 times
18. How can you loop through a list?
a) for i in list:
b) for i in range(list):
c) for i in range(len(list)):
d) Both a and c
Answer
d) Both a and c
19. Which loop is used for iterating over dictionaries?
a) for key in dict:
b) for key, value in dict.items():
c) while key in dict:
d) Both a and b
Answer
d) Both a and b
20. What will this code output?
x = 5
while x > 0:
print(x)
x -= 2
a) 5 3 1
b) 5 4 3 2 1
c) Infinite loop
d) 5 2
Answer
a) 5 3 1
Leave a Reply