Python MCQs on Precedence and Associativity: SET 2

11. What will be the output of the following Python expression?

print(4.00/(2.0+2.0))

a) Error

b) 1.0

c) 1.00

d) 1

Check Answer

Answer: b
The result of the expression shown above is 1.0 because print rounds off digits.

 

 

12. What will be the value of X in the following Python expression?

X = 2+9*((3*12)-8)/10

a) 30.0

b) 30.8

c) 28.4

d) 27.2

Check Answer
Answer: d
The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.

 

 

13. Which of the following expressions involves coercion when evaluated in Python?

a) 4.7 – 1.5

b) 7.9 * 6.3

c) 1.7 % 2

d) 3.4 + 4.6

Check Answer
Answer: c
Coercion is the implicit (automatic) conversion of operands to a common type. Coercion is automatically performed on mixed-type expressions. The expression 1.7 % 2 is evaluated as 1.7 % 2.0 (that is, automatic conversion of int to float).

 

 

14. What will be the output of the following Python expression?

24//6%3, 24//4//2

a) (1,3)

b) (0,3)

c) (1,0)

d) (3,1)

Check Answer

Answer: a
The expressions are evaluated as: 4%3 and 6//2 respectively. This results in the answer (1,3). This is because the associativity of both of the expressions shown above is left to right.

 

 

15. Which among the following list of operators has the highest precedence?

+, -, **, %, /, <<, >>, |

a) <<, >>

b) **

c) |

d) %

Check Answer

Answer: b
The highest precedence is that of the exponentiation operator, that is of **.

 

 

16. What will be the value of the following Python expression?

float(4+int(2.39)%2)

a) 5.0

b) 5

c) 4.0

d) 4

Check Answer

Answer: c
The above expression is an example of explicit conversion. It is evaluated as: float(4+int(2.39)%2) = float(4+2%2) = float(4+0) = 4.0. Hence the result of this expression is 4.0.

 

 

17. Which of the following expressions is an example of type conversion?

a) 4.0 + float(3)

b) 5.3 + 6.3

c) 5.0 + 3

d) 3 + 7

Check Answer

Answer: a
Type conversion is nothing but explicit conversion of operands to a specific type. Options 5.3 + 6.3 and 5.0 + 3 are examples of implicit conversion whereas option 4.0 + float(3) is an example of explicit conversion or type conversion.

 

 

18. Which of the following expressions results in an error?

a) float(‘10’)

b) int(‘10’)

c) float(’10.8’)

d) int(’10.8’)

Check Answer

 

Answer: d
All of the above examples show explicit conversion. However the expression int(’10.8’) results in an error.

 

19. What will be the value of the following Python expression?

4+2**5//10

a) 3

b) 7

c) 77

d) 0

Check Answer
Answer: b
The order of precedence is: **, //, +. The expression 4+2**5//10 is evaluated as 4+32//10, which is equal to 4+3 = 7. Hence the result of the expression shown above is 7.

 

 

20. The expression 2**2**3 is evaluates as: (2**2)**3.

a) True

b) False

Check Answer
Answer: b
The value of the expression (2**2)**3 = 4**3 = 64. When the expression 2**2**3 is evaluated in python, we get the result as 256, because this expression is evaluated as 2**(2**3). This is because the associativity of exponentiation operator (**) is from right to left and not from left to right.

Page  1  2

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment