Python Booleans MCQ Questions and Answers

1. What is the output of bool("False") in Python?

a) True
b) False
c) None
d) Error

Answer:

a) True

Explanation:

In Python, the bool() function converts a value to a Boolean. Any non-empty string, including the string "False", is converted to True.

2. Which of the following statements correctly checks if a variable x is equal to True in Python?

a) if x == True:
b) if x is True:
c) if x:
d) All of the above

Answer:

c) if x:

Explanation:

In Python, the idiomatic way to check if a variable is True is simply "if x:". This implicitly checks for truthiness.

3. What will be the output of the following code?

print(True + True)
a) True
b) 2
c) 1
d) Error

Answer:

b) 2

Explanation:

In Python, the Boolean values True and False are equivalent to 1 and 0, respectively. Therefore, True + True is equivalent to 1 + 1, which is 2.

4. What is the type of the result returned by a logical operator in Python?

a) int
b) float
c) bool
d) str

Answer:

c) bool

Explanation:

Logical operators in Python (and, or, not) return Boolean values (True or False).

5. What does the following expression return: not(False or True)?

a) True
b) False
c) None
d) Error

Answer:

b) False

Explanation:

The expression inside the parentheses evaluates to True, and the 'not' operator negates it, resulting in False.

6. Which of the following is a valid Boolean expression in Python?

a) 1 == 1
b) 1 = 1
c) 1 != 1
d) 1 <> 1

Answer:

a) 1 == 1

Explanation:

In Python, "==" is the equality operator, so 1 == 1 is a valid Boolean expression that evaluates to True.

7. What will be the output of the following code?

x = 10
print(x > 5 and x < 15)
a) True
b) False
c) None
d) Error

Answer:

a) True

Explanation:

The expression checks if x is greater than 5 and less than 15, which is True for x = 10.

8. How do you check if a list is empty in a Pythonic way?

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

Answer:

b) if not my_list:

Explanation:

The most Pythonic way to check if a list is empty is "if not my_list:", which utilizes the truthiness of the list.

9. What is the result of the expression bool(None)?

a) True
b) False
c) None
d) Error

Answer:

b) False

Explanation:

None is considered false in a Boolean context in Python.

10. What is the output of the following code?

y = 0
print(bool(y))
a) True
b) False
c) 0
d) None

Answer:

b) False

Explanation:

The number 0 is considered False in Python, so bool(0) returns False.

Leave a Comment

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

Scroll to Top