Python Questions and Answers – Numeric Types

Q 1. What is the output of print 0.1 + 0.2 == 0.3?
A. True
B. False
C. Machine dependent
D. Error

Show Answer Answer:-B. False
Explanation Explanation:- Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3.

Q 2. Which of the following is not a complex number?
A. k = 2 + 3j
B. k = complex(2, 3)
C. k = 2 + 3l
D. k = 2 + 3J

Show Answer Answer:-C. k = 2 + 3l
Explanation Explanation:- l (or L) stands for long.

Q 3. What is the type of inf?
A. Boolean
B. Integer
C. Float
D. Complex

Show Answer Answer:-C. Float
Explanation Explanation:- Infinity is a special case of floating point numbers. It can be obtained by float(‘inf’).

Q 4. What does ~4 evaluate to?
A. -5
B. -4
C. -3
D. +3

Show Answer Answer:-A. -5
Explanation Explanation:- ~x is equivalent to -(x+1).

Q 5. What does ~~~~~~5 evaluate to?
A. +5
B. -11
C. +11
D. -5

Show Answer Answer:-A. +5
Explanation Explanation:- ~x is equivalent to -(x+1). ~~x = – (-(x+1) + 1) = (x+1) – 1 = x ~~x is equivalent to x Extrapolating further ~~~~~~x would be same as x in the final result. In the question, x value is given as 5 and “~” is repeated 6 times. So, the correct answer for “~~~~~~5” is 5.

Q 6. Which of the following is incorrect?
A. x = 30963
B. x = 0x4f5
C. x = 19023
D. x = 03964

Show Answer Answer:-D. x = 03964
Explanation Explanation:- Numbers starting with a 0 are octal numbers but 9 isn’t allowed in octal numbers.

Q 7. What is the result of cmp(3, 1)?
A. 1
B. 0
C. True
D. False

Show Answer Answer:-A. 1
Explanation Explanation:- cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y.

Q 8. Which of the following is incorrect?
A. float(‘inf’)
B. float(‘nan’)
C. float(’56’+’78’)
D. float(’12+34′)

Show Answer Answer:-D. float(’12+34′)
Explanation Explanation:- ‘+’ cannot be converted to a float.

Q 9. 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

Show Answer Answer:-D. Value depends on Python version
Explanation 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. 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 hence “round(0.5) – (round(-0.5)) = 0 – 0 = 0“. Also note that the round(2.5) is 2 (which is an even number) whereas round(3.5) is 4 (which is an even number).

Q 10. What does 3 ^ 4 evaluate to?
A. 81
B. 12
C. 0.75
D. 7

Show Answer Answer:-D. 7
Explanation Explanation:- ^ is the Binary XOR operator.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page