Python If … Else MCQ Questions and Answers

Python’s if...else statement is a fundamental control flow tool that allows you to execute code based on certain conditions. Here we present 12 multiple-choice questions to test your knowledge of Python if-else statement. Each MCQ has the correct answer with an explanation. 

1. What is the correct syntax for an if statement in Python?

a) if (x > 10) { }
b) if x > 10:
c) if x > 10 then:
d) if (x > 10):

Answer:

b) if x > 10:

Explanation:

In Python, the syntax for an if statement is 'if' followed by the condition, a colon, and then the block of code to execute if the condition is true.

2. How do you check for multiple conditions in a single if statement?

a) if x > 10 and y < 5:
b) if x > 10 && y < 5:
c) if (x > 10) && (y < 5):
d) if x > 10 & y < 5:

Answer:

a) if x > 10 and y < 5:

Explanation:

Python uses 'and' to check if multiple conditions are true.

3. How would you write an if statement that executes when x is not equal to 10?

a) if x != 10:
b) if x =! 10:
c) if x <> 10:
d) if x not 10:

Answer:

a) if x != 10:

Explanation:

The '!=' operator is used in Python to check if two values are not equal.

4. Which keyword is used for executing a block of code if the if condition is false?

a) else
b) otherwise
c) elif
d) except

Answer:

a) else

Explanation:

The 'else' keyword is used in Python to execute a block of code when the if condition is false.

5. How do you implement multiple conditions that trigger different blocks of code?

a) if … elif … else
b) if … else if … else
c) if … then … else
d) if … or … else

Answer:

a) if … elif … else

Explanation:

Python uses 'elif', short for 'else if', for multiple, mutually exclusive conditions in an if statement.

6. What is the output of the following code?

x = 10
if x > 10:
print("Greater")
elif x == 10:
print("Equal")
else:
print("Lesser")
a) Greater
b) Equal
c) Lesser
d) No output

Answer:

b) Equal

Explanation:

The code checks if x is greater than, equal to, or less than 10. Since x is 10, it prints "Equal".

7. How do you write a one-line if statement in Python?

a) if x > 10: print(x)
b) print(x) if x > 10
c) print(x) where x > 10
d) (print x) if x > 10

Answer:

b) print(x) if x > 10

Explanation:

Python allows one-line if statements, where the action is before the condition, separated by 'if'.

8. What is the correct way to check if a list is empty in an if statement?

a) if len(my_list) == 0:
b) if my_list == []:
c) if not my_list:
d) All of the above

Answer:

d) All of the above

Explanation:

All three methods are correct for checking if a list is empty in Python.

9. Can an if statement contain only an else clause without elif?

a) Yes
b) No
c) Only inside a function
d) Only if there is no code in the else clause

Answer:

a) Yes

Explanation:

An if statement can have an else clause without an elif, as elif is optional.

10. What is the result of the following code?

x = 5
if x > 10:
print("A")
else:
if x > 7:
print("B")
else:
print("C")
a) A
b) B
c) C
d) No output

Answer:

c) C

Explanation:

The code uses nested if statements. Since x is 5, which is not greater than 10 or 7, it prints "C".

11. Which keyword allows you to check if a condition is false in an if statement?

a) not
b) false
c) invert
d) negate

Answer:

a) not

Explanation:

The 'not' keyword is used to check if a condition is false in Python.

12. How do you check for the existence of a key in a dictionary within an if statement?

a) if my_dict.has_key('key'):
b) if 'key' in my_dict:
c) if my_dict['key']:
d) if my_dict.exists('key'):

Answer:

b) if 'key' in my_dict:

Explanation:

The syntax 'if key in dictionary:' is used to check if a key exists in a dictionary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top