All sample questions / NumPy
NumPy Arrays, dtypes and Scalars, what does print(a) print after a = [np.int64(1), np.int64(2)]?
Block 1, Arrays, dtypes and Scalars, question 2 of the NumPy sample
import numpy as np
a = [np.int64(1), np.int64(2)]
print(a)
What does this print?
Show the answer and why each option is right or wrong
Answer, A. [np.int64(1), np.int64(2)]
- A. [np.int64(1), np.int64(2)]
- Correct. Printing a plain Python list calls repr on each element, so the scalars show their NumPy 2 type wrapper even though print itself uses str on the outer list.
- B. ['np.int64(1)', 'np.int64(2)']
- Assumes the wrapped reprs come out as quoted text rather than as the literal repr NumPy produces.
- C. [int64(1), int64(2)]
- Assumes the wrapper drops the np module prefix inside a list the way it never does for a bare scalar either.
- D. [1, 2]
- Assumes the elements print the same bare way they would if printed on their own with print.
A Python list has no special display rule of its own, it falls back to calling repr on every element it contains. Since NumPy 2 scalar repr includes the type name, elements inside a list show that wrapper even when printing the same scalar alone would only show the bare value.
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.