1.

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 versionI got this question in an online interview.My doubt stems from Numeric Types topic in division Variable Names, Operators, Data Types & Numeric Types of Python

Answer» RIGHT choice is (d) Value depends on Python version

Easiest explanation - 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.


Discussion

No Comment Found

Related InterviewSolutions