Contents

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

c

Explanation: 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).


What will be the output of the following Python expression?
a) (1,3)                                  
b) (0,3)
c) (1,0)                                  
d) (3,1)

a

Explanation: 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.


Which among the following list of operators has the highest precedence?
a) <<, >>                               
b) **
c) |                                                          
d) %

b

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


What will be the value of the following Python expression?
a) 5.0                                     
b) 5
c) 4.0                                     
d) 4

c

Explanation: 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.


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

a

Explanation: 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.


Which of the following expressions results in an error?
a) float(‘10’)                        
b) int(‘10’)
c) float(’10.8’)     
d) int(’10.8’)

d

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