All sample questions / NumPy
NumPy Arrays, dtypes and Scalars, what does print(a.dtype) print after a = np.array([1, 2.5, True])?
Block 1, Arrays, dtypes and Scalars, question 1 of the NumPy sample
import numpy as np
a = np.array([1, 2.5, True])
print(a.dtype)
What does this print?
Show the answer and why each option is right or wrong
Answer, B. float64
- A. int64
- Assumes the two non float values outvote the single float and the dtype settles on int64.
- B. float64
- Correct. With bool, int and float all present, NumPy still promotes to the widest of the three, float64.
- C. bool
- Assumes the presence of a boolean anywhere forces the narrower bool dtype regardless of the other values.
- D. object
- Assumes a three way mix of value kinds is too ambiguous for a numeric dtype and falls back to object.
The promotion rule does not count votes, it only asks which dtype among those present can hold every value without loss. With int, float and bool together, float64 is that dtype, exactly as it would be with only two of the three kinds present.
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.
Did this save you a lookup? Buy me a coffee on Ko-fi.