Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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
   False
View Answer

Answer: d
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 - Question

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

Answer: c
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 - Question

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
   False
View Answer

Answer: b
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 - Question

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

Answer: a
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 - Question

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

Answer: d
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 - Question

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

Answer: c
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 - Question

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

Answer: b
Explanation: If the truth method is not defined, the object is considered true. Hence the output of the code shown above is true.




8 - Question

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

Answer: c
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 - Question

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

Answer: d
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 - Question

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

Answer: b
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.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners