Welcome to the Python Loops Quiz!
Loops are a elementary idea in Python that assist automate repetitive duties.
This quiz will take a look at your understanding of for
loops, whereas
loops, loop management statements, and iteration strategies. Get able to loop by means of these questions and take a look at your abilities!
Desk of Contents:
- Python quizzes for novices sequence
Loops Quiz
Quiz Questions
1. What’s the primary goal of a loop in Python?
a) To repeat a block of code a number of occasions
b) To outline a perform
c) To create a variable
d) To deal with exceptions
Reply
a) To repeat a block of code a number of occasions
2. Which of the next is a legitimate for
loop in Python?
a) for i = 1 to five:
b) for i in vary(5):
c) loop(i, 5):
d) for(i=0; i<5; i++)
Reply
b) for i in vary(5):
3. What would be the output of this loop?
for i in vary(3):
print("Whats up")
a) Prints “Whats up” 3 occasions
b) Prints “HelloHelloHello”
c) Syntax error
d) Infinite loop
Reply
a) Prints “Whats up” 3 occasions
4. What number of occasions will the next whereas
loop execute?
rely = 0
whereas rely < 5:
print(rely)
rely += 1
a) 4
b) 5
c) Infinite occasions
d) 6
Reply
b) 5
5. What key phrase is used to exit a loop prematurely?
a) exit
b) break
c) cease
d) proceed
Reply
b) break
6. What does the proceed
assertion do in a loop?
a) Stops the loop execution
b) Skips the present iteration and strikes to the subsequent
c) Repeats the present iteration
d) Not one of the above
Reply
b) Skips the present iteration and strikes to the subsequent
7. What would be the output of this loop?
for i in vary(3, 8, 2):
print(i)
a) 3 5 7
b) 3 4 5 6 7
c) 3 5
d) 3 5 7 9
Reply
a) 3 5 7
8. Which loop is assured to execute a minimum of as soon as?
a) for
loop
b) whereas
loop
c) do-while
loop
d) Not one of the above
Reply
c) do-while
loop (Python doesn’t have a do-while
loop, however in different languages, it runs a minimum of as soon as)
9. What is going to occur if the loop situation by no means turns into False
?
a) The loop will execute as soon as
b) The loop will run infinitely
c) Python will throw an error
d) The loop will skip
Reply
b) The loop will run infinitely
10. What is going to this code output?
for i in vary(2):
for j in vary(2):
print(i, j)
a) (0,0) (0,1) (1,0) (1,1)
b) (0,0) (1,1)
c) (0,1) (1,0)
d) Not one of the above
Reply
a) (0,0) (0,1) (1,0) (1,1)
11. How do you create an infinite loop?
a) whereas True:
b) for i in vary(inf):
c) whereas i != 100:
d) for i in vary(0, -1):
Reply
a) whereas True:
12. What’s the output of this loop?
for i in vary(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) Not one of the above
Reply
b) 5 4 3 2 1
13. Which of the next is NOT a legitimate loop management assertion?
a) break
b) proceed
c) cross
d) skip
Reply
d) skip
14. What would be the output?
for i in vary(3):
print(i)
else:
print("Completed")
a) 0 1 2 Completed
b) 0 1 2
c) Completed
d) Error
Reply
a) 0 1 2 Completed
15. Which perform generates a sequence of numbers in loops?
a) listing()
b) vary()
c) sequence()
d) numbers()
Reply
b) vary()
16. What does the enumerate()
perform do in a loop?
a) Counts iterations
b) Returns index and worth of an iterable
c) Prints numbers
d) Stops the loop
Reply
b) Returns index and worth of an iterable
17. What does the next loop do?
for _ in vary(3):
print("Python")
a) Prints “Python” 3 occasions
b) Prints an error
c) Prints “Python” as soon as
d) Not one of the above
Reply
a) Prints “Python” 3 occasions
18. How are you going to loop by means of an inventory?
a) for i in listing:
b) for i in vary(listing):
c) for i in vary(len(listing)):
d) Each a and c
Reply
d) Each a and c
19. Which loop is used for iterating over dictionaries?
a) for key in dict:
b) for key, worth in dict.gadgets():
c) whereas key in dict:
d) Each a and b
Reply
d) Each a and b
20. What is going to this code output?
x = 5
whereas x > 0:
print(x)
x -= 2
a) 5 3 1
b) 5 4 3 2 1
c) Infinite loop
d) 5 2
Reply
a) 5 3 1
How did you do? 🎯
- 18-20 right → 🏆 Wonderful! You’re a Python features professional!
- 14-17 right → 👍 Nice job! Hold working towards.
- 10-13 right → 🙂 Good, however there’s room for enchancment.
- Beneath 10 → 🤔 No worries! Overview the ideas and take a look at once more.