python-control-structures-quiz

Python’s control structures are essential for directing the flow of your programs, allowing you to make decisions, repeat actions, and manage complex logic with ease. This quiz will test your grasp of these fundamental concepts, ensuring you can write efficient and effective Python code. Good luck, and may your logic be flawless!
We recommend that you do not leave the page that you are taking this quiz in. Stay honest 🙂
Python Control Structures Quiz
0% Complete
1 of 13
1. Which of the following is the correct syntax for an if-else statement in Python?
Choose the correct syntax for a basic if-else statement.

Python Control Structures Quiz Questions Overview

1. Which of the following is the correct syntax for an if-else statement in Python?

if x > 10: else:
if x > 10 then else:
if x > 10: else
if x > 10: else:

2. What is the output of the following code? for i in range(3): print(i)

0 1 2
1 2 3
0 1 2 3
None of the above

3. Which keyword is used to exit a loop prematurely in Python?

exit
break
continue
stop

4. What is the purpose of the continue statement in a loop?

To exit the loop
To skip the rest of the code inside the loop for the current iteration
To stop the program
To repeat the loop from the beginning

5. How do you write a while loop that runs as long as x is less than 10?

while x < 10:
while (x < 10)
while x < 10
while (x < 10):

6. Which of the following is the correct way to write an if-elif-else statement?

if x == 1: elif x == 2: else:
if x == 1: elif x == 2 else:
if x == 1: elif x == 2: else:
if x == 1 elif x == 2 else:

7. What will be the output of the following code? x = 5 if x > 2: print(‘Greater’) else: print(‘Lesser’)

Greater
Lesser
Error
None of the above

8. Which of the following loops will execute at least once, even if the condition is false?

for loop
while loop
do-while loop
None of the above

9. What is the keyword used to define a function in Python?

func
define
def
function

10. What will be the output of the following code? x = 10 if x > 5: print(‘High’) if x < 15: print('Medium') else: print('Low')

High
Medium
High Medium
Low

11. How do you create an infinite loop in Python?

while True:
while (True)
for True:
for (True)

12. Which of the following is NOT a valid Python loop control statement?

break
continue
pass
skip

13. What will be the output of the following code? for i in range(2): for j in range(2): print(i, j)

0 0 0 1 1 0 1 1
0 0 1 1
0 0 1 1 2 2
0 0 0 1 1 0 1 1 2 2
We recommend that you do not leave the page that you are taking this quiz in. Stay honest 🙂
We have chosen the next challenge for you. Take the next test:

Can Your Friends Do Better Than You in This Quiz?

Share this quiz with your friends and compare results.

Was this page helpful?