Test Series - python

Test Number 5/108

Q: Which of the following is incorrect?
A. x = 0b101
B. x = 0x4f5
C. x = 19023
D. x = 03964
Solution: Numbers starting with a 0 are octal numbers but 9 isn’t allowed in octal numbers.
Q: What is the result of cmp(3, 1)?
A. 1
B. 0
C. True
D. False
Solution: cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.
Q: Which of the following is incorrect?
A. float(‘inf’)
B. float(‘nan’)
C. float(’56’+’78’)
D. float(’12+34′)
Solution: ‘+’ cannot be converted to a float.
Q: What is the result of round(0.5) – round(-0.5)?
A. 1.0
B. 2.0
C. 0.0
D. Value depends on Python version
Solution: The behavior of the round() function is different in Python 2 and Python 3. In Python 2, it rounds off numbers away from 0 when the number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is -1 whereas in Python 3, it rounds off numbers towards nearest even number when the number to be rounded off is exactly halfway through. See the below output.
Here’s the runtime output for Python version 2.7 interpreter.

$ python
Python 2.7.17 (default, Nov  7 2019, 10:07:09)
>>> round(0.5)
1.0
>>> round(-0.5)
-1.0
>>>
In the above output, you can see that the round() functions on 0.5 and -0.5 are moving away from 0 and hence “round(0.5) – (round(-0.5)) = 1 – (-1) = 2”

Here’s the runtime output for Python version 3.6 interpreter.

$ python3
Python 3.6.8 (default, Oct  7 2019, 12:59:55)
>>> round(0.5)
0
>>> round(-0.5)
0
>>> round(2.5)
2
>>> round(3.5)
4
>>>
In the above output, you can see that the round() functions on 0.5 and -0.5 are moving towards 0 and 
Q: What does 3 ^ 4 evaluate to?
A. 81
B. 12
C. 0.75
D. 7
Solution: ^ is the Binary XOR operator.

You Have Score    /5