Welcome to the Python Loops Quiz!
Loops are a basic idea in Python that assist automate repetitive duties.
This quiz will check your understanding of for
loops, whereas
loops, loop management statements, and iteration strategies. Get able to loop via these questions and check your abilities!
Desk of Contents:
- Python quizzes for rookies sequence
Loops Quiz
Quiz Questions
1. What’s the principal objective of a loop in Python?
a) To repeat a block of code a number of instances
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 instances
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 instances
b) Prints “HelloHelloHello”
c) Syntax error
d) Infinite loop
Reply
a) Prints “Whats up” 3 instances
4. What number of instances will the next whereas
loop execute?
rely = 0
whereas rely < 5:
print(rely)
rely += 1
a) 4
b) 5
c) Infinite instances
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 at the least 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 at the least 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("Performed")
a) 0 1 2 Performed
b) 0 1 2
c) Performed
d) Error
Reply
a) 0 1 2 Performed
15. Which perform generates a sequence of numbers in loops?
a) checklist()
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 instances
b) Prints an error
c) Prints “Python” as soon as
d) Not one of the above
Reply
a) Prints “Python” 3 instances
18. How will you loop via an inventory?
a) for i in checklist:
b) for i in vary(checklist):
c) for i in vary(len(checklist)):
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