1.

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.6I have been asked this question at a job interview.This intriguing question comes from Precedence and Associativity in portion Precedence and Associativity, Bitwise & Boolean of Python

Answer»

The correct choice is (c) 1.7 % 2

To explain I would say: 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).



Discussion

No Comment Found

Related InterviewSolutions