All sample questions / NumPy
What does print(b.base is None) print? NumPy Views, Copies and Reshape
Block 4, Views, Copies and Reshape, question 9 of the NumPy sample
import numpy as np
a = np.arange(6)
b = a[[1, 2, 3]]
print(b.base is None)
What does this print?
Show the answer and why each option is right or wrong
Answer, B. True
- A. False
- Assumes any array built from indexing another array must carry a base pointing back to it.
- B. True
- Correct. Fancy indexing produces an independent array that owns its own memory, so it has no source array to point back to and its base is None.
- C. AttributeError
- Assumes an array produced by indexing has no base attribute to check at all.
- D. None
- Assumes base reports the missing marker None as a printed value instead of the array being asked whether its base is that value.
The base attribute distinguishes an array that owns its memory from one that only views someone else's. Because fancy indexing always copies, the result owns its own freshly allocated buffer and its base is None, exactly like an array built directly with a constructor.
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.