This set of Python Developer Questions & Answers focuses on “Mathematical Functions”.
1. What does the function math.frexp(x) return?
a) a tuple containing the mantissa and the exponent of x
b) a list containing the mantissa and the exponent of x
c) a tuple containing the mantissa of x
d) a list containing the exponent of x
View Answer
Explanation: It returns a tuple with two elements. The first element is the mantissa and the second element is the exponent.
2. What is the result of math.fsum([.1 for i in range(20)])?
a) 2.0
b) 20
c) 2
d) 2.0000000000000004
View Answer
Explanation: The function fsum returns an accurate floating point sum of the elements of its argument.
3. What is the result of sum([.1 for i in range(20)])?
a) 2.0
b) 20
c) 2
d) 2.0000000000000004
View Answer
Explanation: There is some loss of accuracy when we use sum with floating point numbers. Hence the function fsum is preferable.
4. What is returned by math.isfinite(float(‘inf’))?
a) True
b) False
c) None
d) error
View Answer
Explanation: float(‘inf’) is not a finite number.
5. What is returned by math.isfinite(float(‘nan’))?
a) True
b) False
c) None
d) error
View Answer
Explanation: float(‘nan’) is not a finite number.
6. What is x if x = math.isfinite(float(‘0.0’))?
a) True
b) False
c) None
d) error
View Answer
Explanation: float(‘0.0’) is a finite number.
7. What will be the output of the following Python code?
>>> -float('inf') + float('inf')
a) inf
b) nan
c) 0
d) 0.0
View Answer
Explanation: The result of float(‘inf’)-float(‘inf’) is undefined.
8. What will be the output of the following Python code?
print(math.isinf(float('-inf')))
a) error, the minus sign shouldn’t have been inside the brackets
b) error, there is no function called isinf
c) True
d) False
View Answer
Explanation: -float(‘inf’) is the same as float(‘-inf’).
9. What is the value of x if x = math.ldexp(0.5, 1)?
a) 1
b) 2.0
c) 0.5
d) none of the mentioned
View Answer
Explanation: The value returned by ldexp(x, y) is x * (2 ** y). In the current case x is 1.0.
10. What is returned by math.modf(1.0)?
a) (0.0, 1.0)
b) (1.0, 0.0)
c) (0.5, 1)
d) (0.5, 1.0)
View Answer
Explanation: The first element is the fractional part and the second element is the integral part of the argument.
Sanfoundry Global Education & Learning Series – Python.
To practice all developer questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Python Books
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship