This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Boolean”.
1. What will be the output of the following Python code snippet?
bool(‘False’) bool()
a)
True True
b)
False True
c)
False False
d)
True FalseView Answer
Explanation: The Boolean function returns true if the argument passed to the bool function does not amount to zero. In the first example, the string ‘False’ is passed to the function bool. This does not amount to zero and hence the output is true. In the second function, an empty list is passed to the function bool. Hence the output is false.
2. What will be the output of the following Python code snippet?
['hello', 'morning'][bool('')]
a) error
b) no output
c) hello
d) morning
View Answer
Explanation: The line of code shown above can be simplified to state that ‘hello’ should be printed if the argument passed to the Boolean function amounts to zero, else ‘morning’ will be printed.
3. What will be the output of the following Python code snippet?
not(3>4) not(1&1)
a)
True True
b)
True False
c)
False True
d)
False FalseView Answer
Explanation: The function not returns true if the argument amounts to false, and false if the argument amounts to true. Hence the first function returns false, and the second function returns false.
4. What will be the output of the following Python code?
['f', 't'][bool('spam')]
a) t
b) f
c) No output
d) Error
View Answer
Explanation: The line of code can be translated to state that ‘f’ is printed if the argument passed to the Boolean function amount to zero. Else ‘t’ is printed. The argument given to the Boolean function in the above case is ‘spam’, which does not amount to zero. Hence the output is t.
5. What will be the output of the following Python code?
l=[1, 0, 2, 0, 'hello', '', []] list(filter(bool, l))
a) Error
b) [1, 0, 2, 0, ‘hello’, ”, []]
c) [1, 0, 2, ‘hello’, ”, []]
d) [1, 2, ‘hello’]
View Answer
Explanation: The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, ‘hello’].
6. What will be the output of the following Python code if the system date is 21st June, 2017 (Wednesday)?
[] or {} {} or []
a)
[] {}
b)
[] []
c)
{} []
d)
{} {}View Answer
Explanation: The code shown above shows two functions. In both the cases the right operand is returned. This is because each function is evaluated from left to right. Since the left operand is false, it is assumed that the right operand must be true and hence the right operand is returned in each of the above case.
7. What will be the output of the following Python code?
class Truth: pass x=Truth() bool(x)
a) pass
b) true
c) false
d) error
View Answer
Explanation: If the truth method is not defined, the object is considered true. Hence the output of the code shown above is true.
8. What will be the output of the following Python code?
if (9 < 0) and (0 < -9): print("hello") elif (9 > 0) or False: print("good") else: print("bad")
a) error
b) hello
c) good
d) bad
View Answer
Explanation: The code shown above prints the appropriate option depending on the conditions given. The condition which matches is (9>0), and hence the output is: good.
9. Which of the following Boolean expressions is not logically equivalent to the other three?
a) not(-6<0 or-6>10)
b) -6>=0 and -6<=10
c) not(-6<10 or-6==10)
d) not(-6>10 or-6==10)
View Answer
Explanation: The expression not(-6<0 or -6>10) returns the output False.
The expression -6>=0 and -6<=10 returns the output False.
The expression not(-6<10 or -6==10) returns the output False.
The expression not(-6>10 or -6==10) returns the output True.
10. What will be the output of the following Python code snippet?
not(10<20) and not(10>30)
a) True
b) False
c) Error
d) No output
View Answer
Explanation: The expression not(10<20) returns false. The expression not(10>30) returns true. The and operation between false and true returns false. Hence the output is false.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs
- Apply for Python Internship
- Check Information Technology Books