Python Questions and Answers – Math – 2

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

Answer: a
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

Answer: a
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

Answer: d
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

Answer: b
Explanation: float(‘inf’) is not a finite number.
advertisement
advertisement

5. What is returned by math.isfinite(float(‘nan’))?
a) True
b) False
c) None
d) error
View Answer

Answer: b
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

Answer: a
Explanation: float(‘0.0’) is a finite number.

7. What will be the output of the following Python code?

advertisement
>>> -float('inf') + float('inf')

a) inf
b) nan
c) 0
d) 0.0
View Answer

Answer: b
Explanation: The result of float(‘inf’)-float(‘inf’) is undefined.
advertisement

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

Answer: c
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

Answer: d
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

Answer: a
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.