30 Python Conditional Statements MCQ

Welcome to our Python conditional statements MCQ quiz! Here, we have listed the best collection of important questions based on Python conditional statements in quiz format, with answers and explanations.

This Multiple-Choice Question (MCQ) is designed to test your knowledge of conditional statements, such as if statement, if-else, etc. in Python from basic syntax to advanced concepts. Whether you’re preparing for a job interview, a coding competition, or just want to sharpen your coding skills, these multiple-choice questions will challenge your understanding.

Each question has four options and you have to choose the correct one. There is no time limit to answer the questions. However, you can set a time limit for yourself to simulate exam conditions. You can also bookmark this Python conditional statements MCQ to revise before exams!

Are you ready to test your knowledge on conditional statements in Python? Let’s see if you can score 30/30—start now with question 1! 👇

1. What is the correct syntax for a basic if statement in Python?
A. if x = 5:
B. if (x == 5)
C. if x == 5:
D. if x = 5
c
In Python, the correct syntax uses double equals == for comparison and a colon : to start the block.
2. How do you write an if-else statement in Python?
A. if condition: print(‘Yes’) else: print(‘No’)
B. if condition print(‘Yes’); else print(‘No’)
C. if condition: print(‘Yes’); print(‘No’)
D. if condition: print(‘Yes’) else print(‘No’)
a
Python uses indentation and colons to structure conditional blocks properly.
3. What is the output of the following code?
x = 10
y = 20
if x > y:
    print("X is greater")
else:
    print("Y is greater")
A. X is greater
B. Y is greater
C. Error
D. None
b
Since x is not greater than y, the else block will execute.
4. What does the following code output?
x = 0
if x:
    print("True")
else:
    print("False")
A. True
B. False
C. TypeError
D. None
b
In Python, 0 is treated as False in a conditional statement.
5. Which operator is used to combine multiple conditions?
A. && and ||
B. & and |
C. and, or
D. All of the above
c
Python uses and and or as logical operators for combining more than one condition.
6. Which of the following is correct syntax to use ternary operator with if-else statement in Python?
A. value if other_value else condition
B. if condition ? value : other_value
C. if condition then value else other_value
D. value if condition else other_value
d
Python uses a unique ternary operator format.
7. What is the output of the below code?
x = 10
y = 20
if x > 5:
    if y < 15:
        print("Case 1")
    else:
        print("Case 2")
else:
    print("Case 3")
A. Case 1
B. Case 2
C. Case 3
D. None
b
The outer if condition is True, but the inner if condition is False, so the else block prints "Case 2".
8. Which of following statement is correct about Python's elif?
A. It is optional and can replace if.
B. It must be used after an else block.
C. It is used to check multiple conditions.
D. Python does not support elif.
c
elif is used to add multiple conditional checks in sequence.
9. What is the output of the following code if no error?
x = 5
if x == 5 or x / 0:
    print("No Error")
A. No Error
B. ZeroDivisionError
C. TypeError
D. None of these
a
In Python, or uses short-circuiting. Since x == 5 is True, x / 0 is never evaluated.
10. What is the output of this code if not error found?
x = False
y = True
if x == y:
    print("Equal")
elif x:
    print("X is True")
else:
    print("X is False")
A. Equal
B. X is True
C. X is False
D. None
c
x == y is False, x itself is False, so the else block executes.
11. How many times will "Hello" be printed?
x = 5
if x > 1:
    print("Hello")
if x > 3:
    print("Hello")
elif x > 2:
    print("Hello")
else:
    print("Hello")
A. 1
B. 2
C. 3
D. 4
b
The first two if statements are True, but the elif block won't execute because the previous if was already True.
12. What is the output if no error found?
x = 0
if x:
    print("A")
elif not x:
    if x is not None:
        print("B")
    else:
        print("C")
else:
    print("D")
A. A
B. B
C. C
D. D
c
x is 0, which means false, so first condition fails. not x is True, then x is not None is False because 0 represents not None. Therefore, "C" prints.
13. What will be the output of the below code snippet if no error?
x = 10
if x > 5:
    pass
elif x > 7:
    print("A")
else:
    print("B")
print("C")
A. A
B. B
C. C
D. A C
c
The if condition is True but has a pass. The rest are skipped, and "C" prints after the if-elif-else block.
14. What is the output of the below Python code?
x = 5
y = 10
if x < y: print("A"); print("B")
else: print("C"); print("D")
A. A
B. A B
C. C D
D. TypeError
b
Multiple statements can be on one line separated by semicolons. Both print statements execute when x < y is True.
15. Which of these is not equivalent to the others?
A. if not (a and b)
B. if not a or not b
C. if a == False or b == False
D. if not a and not b
d
Options a, b, and c are equivalent by De Morgan's laws. Option d is different as it requires both to be False.
16. Which operator is used for equality comparison in Python?
A. =
B. ==
C. ===
D. !=
b
The == operator checks for equality, while = is for assignment.
17. How can this code be rewritten using a ternary operator?
if len(name) > 5:
    size = "Long"
else:
    size = "Short"
A. size = len(name) > 5 ? "Long" : "Short"
B. size = if len(name) > 5 then "Long" else "Short"
C. size = "Long" when len(name) > 5 otherwise "Short"
D. size = "Long" if len(name) > 5 else "Short"
d
This is the correct Python ternary operator syntax.
18. What is the output of this code?
a = False
b = True
if a and b or not a and b:
    print("A")
elif a or not b:
    print("B")
else:
    print("C")
A. A
B. B
C. C
D. No output
a
The condition evaluates as (False and True) or (not False and True) → False or (True and True) → False or True → True.
19. Which of the following condition properly checks if a number is between 10 and 20 (exclusive)?
A. if 10 < x < 20:
B. if x > 10 and x < 20:
C. if x in range(11, 20):
D. All of the above
d
All options correctly check the range. Python allows chained comparisons (a), logical operators (b), and range checking (c).
20. Which of the following output is correct for this code?
x = 0
if x:
    print("A")
if not x:
    print("B")
if x == False:
    print("C")
if x is False:
    print("D")
A. A B C D
B. B C
C. B
D. B C D
b
In Python, 0 is considered as False in a boolean context. Therefore, first condition is False, and "A" is not printed. The condition not x checks if x is False. Since x = 0 is False, this condition is True, and "B" is printed. The condition x == False performs a value comparison. Since 0 == False is True, this condition is True, and "C" is printed. The last condition x is False checks for identity, not value. Since 0 is an integer, while False is a boolean type, 0 is False is False. So, "D" is not displayed.
21. What will be the output of below code if no error found?
values = [1, 2, 3]
if values:
    print("A")
if len(values) > 0:
    print("B")
if values is not None:
    print("C")
A. A
B. C
C. A B
D. A B C
d
In Python, non-empty lists are considered as True in a boolean context. Since values = [1, 2, 3] is non-empty, this condition is True and "A" is displayed. When Python executes if len(values) > 0:, the len(values) returns 3, which is greater than 0. So, this condition is also True, and "B" is also displayed. The condition values is not None: in if statement checks if the variable exists and is not None. Since values = [1, 2, 3] is a valid list, and it is not None, so this condition is True, and "C" is printed.
22. Which of these is not a valid conditional statement in Python?
A. if (x := get_value()) > 10:
B. if x = 10:
C. if x > 10 and y < 20:
D. if all([x > 10, y < 20, z == 5]):
b
In the first option, there is a walrus operator := (introduced in Python 3.8) which allows assignment within a conditional. So, this is valid. In the second option, x = 10 is an assignment, not a comparison. In conditional statements, you must use == for equality checking. This is not valid, hence it is the correct answer. In the third option, there is also a valid conditional statement that checks if x is greater than 10 and y is less than 20. Option d also contains the conditional statements. So, it is also valid.
23. If there is no error in the below code snippet, then what will be the output?
values = [0, False, '', None, [], {}]
count = 0
for item in values:
    if not item:
        count += 1
print(count)
A. 1
B. 3
C. 5
D. 6
d
All values in the list are falsy values, such as 0, False, empty string, None, empty list, empty dict. Therefore, the count increments for each item.
24. Which of these evaluates differently from the others?
x = []
A. if not x:
B. if x == False:
C. if len(x) == 0:
D. if x is None:
d
In Python, an empty list represents False, so if not x: is True when x = []. This condition evaluates to True. In the second option, the condition x == False performs a value comparison, and bool([ ]) is False. So, x == False is True. In the third option, the condition len(x) == 0 checks the length of the list, which is 0 for an empty list. So, this is also True. In the last option, the condition x is None checks for identity, not just value. Since an empty list [ ] is not the same object as None, so this evaluates to False. Hence, the final answer is d.
25. What will be the output if there is no error in this code?
x = 5
y = 10
if x > 2 and y < 20 or x + y == 15:
    print("A")
else:
    print("B")
A. A
B. B
C. Both A and B
D. Error
a
Due to operator precedence, this evaluates as (x > 2 and y < 20) or (x + y == 15). Both parts are True because 5 > 2 and 10 < 20, and 5 + 10 = 15.
26. If there is no error in the below code, what will be the result?
x = 5
if 1 < x < 10:
    print("Hello")
if (1 < x) < 10:
    print("Hi")
A. Hello
B. Hi
C. Hello Hi
D. Error
c
The first is proper chained comparison, which gives True value. The second evaluates as (1 < 5) < 10 → True < 10 → 1 < 10 → True.
27. What does this code output?
if (x := 0) or (y := 1):
    print(x, y)
else:
    print(y, x)
A. 0 1
B. 1 0
C. 0 0
D. 1 1
a
The walrus operator assigns values during the check. The first condition x := 0 evaluates to False, while the second condition y := 1 evaluates to True. So, when the if block will execute, it will display x = 0 and y = 1.
28. What will be the result of the code snippet if no error in the below code?
def test():
    print("Called")
    return False

if True or test():
    print("One")
if False and test():
    print("Two")
A. One
B. Two
C. Called One
D. One Two
a
In Python, True or test() uses short-circuit evaluation. The behavior of the OR operator is such that if the first condition is True, the second part is never evaluated. Since True is the first condition, the function test() will not be called. Therefore, "Called" is not displayed. When Python interpreter will execute if block, "One" will display.

The condition False and test() will also use short-circuit evaluation. In Python, the behavior of the AND operator is such that if the first condition is False, the second part is never evaluated. Since False is the first condition, the function test() will not be called. So, "Called" is not displayed. Thus, if block does not execute, and "B" is not displayed.
29. What makes an object evaluate to False in a Python conditional?
A. The object has a __bool__() method returning False.
B. The object has a __len__() method returning 0.
C. The object is None or numerically equal to 0.
D. All of the above
d
All options are correct.
30. When should you use the walrus operator (:=) in conditionals?
A. When you need to assign and check a value in one line.
B. When you want to make code more concise by avoiding redundant lines.
C. When the assigned value will be reused later
D. All of the above
d
The walrus operator (:=), introduced in Python 3.8, allows you to assign a value to a variable while using it in an expression. It is useful in all the above cases.

Quiz Results

0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score
You can do better next time!