All sample questions / NumPy
NumPy NaN, Overflow and Type Promotion, what does print(a.max()) print after a = np.array([1.0, np.nan, 3.0])?
Block 6, NaN, Overflow and Type Promotion, question 14 of the NumPy sample
import numpy as np
a = np.array([1.0, np.nan, 3.0])
print(a.max())
What does this print?
Show the answer and why each option is right or wrong
Answer, A. nan
- A. nan
- Correct. Ordinary max() does not skip NaN, so its presence anywhere in the array poisons the whole comparison and the result is NaN.
- B. ValueError
- Assumes comparing against a NaN is invalid and must raise an error.
- C. 3.0
- Assumes max() quietly ignores the NaN and reports the largest real value instead.
- D. 1.0
- Assumes max() reports the smallest real value rather than propagating the NaN.
Just like sum() and mean(), the plain max() reduction propagates a NaN rather than skipping it, since any comparison against NaN is neither true nor false in a useful sense. np.nanmax exists specifically to ignore NaN entries when they should not count.
The answer above is what NumPy 2.4.4 printed when the snippet was run, not what someone expected it to do.
This question comes from a bank of 279 NumPy interview questions, all built the same way, sold as one PDF with the full answer key.
Get the 279 NumPy questions, 9 euros
Or SQL, Python, pandas and NumPy together, 1,636 questions, 19 euros
Or play the free NumPy sample, twenty questions in the browser, also as a free PDF.
Previous question Next question
Did this save you a lookup? Buy me a coffee on Ko-fi.