Python Conditional Execution Quiz - With answers.
Each of these questions is designed to challenge your understanding of Python’s conditional execution.
Question 1.
What is the main purpose of conditional execution in Python?
Question 2.
Which of the following statements about if
statements is FALSE?
Question 3.
What is the output of the following code?
x = None
if x:
print("True")
else:
print("False")
Question 4.
What is the correct way to check if a variable x
has a value of either 10 or 20?
Question 5.
What does if __name__ == "__main__":
do in Python?
Question 6.
How does the try-except
block relate to conditional execution?
Question 7.
What is the output of the following code?
a = 5
b = 10
c = a if a > b else b
print(c)
Question 8.
What is the output of the following code?
x = 3
match (x % 2):
case 0:
print('Even')
case 1:
print('Odd')
case _:
print('Unknown')
Question 9.
What is the output of the following code?
data = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
result = [n for n in data if (n % 2 == 0) if (n % 3 == 0)]
print(result)
Question 10.
What is short-circuit evaluation in Python conditionals?
Question 11.
What is the output of the following code?
x = 10
if x > 5:
print("A")
if x > 15:
print("B")
else:
print("C")
Question 12.
What is the output of the following code?
x = 10
if x > 5:
pass
else:
print("B")
print("A")
Question 13.
What is the output of the following code?
x = 9
if (x // 3) == 3 or (x // 0) == 0:
print("pass")
else:
print("fail")
Question 14.
Which of the following keywords is not associated with conditional execution in Python?
Question 15.
What is the output of the following code?
x = 15
if x > 10:
if x < 20:
print("A")
else:
print("B")
else:
print("C")