NumPy Essentials — Arrays & Vectorization — Concept Check
Questions on NumPy Essentials — Arrays & Vectorization. NumPy arrays store homogeneous data in contiguous memory and operate on whole arrays at C speed — vectorization, broadcasting and boolean masks replace Python loops.
Questions in this quiz
8 questions on NumPy Essentials — Arrays & Vectorization — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A data scientist wants to add 10 to every element in a NumPy array of scores. Which approach will achieve this efficiently?
- AConvert the array to a list, modify it, and convert back.
- BUse the expression 'scores + 10'.
- CUse a for loop to iterate and add 10 to each element.
- DUse a list comprehension to add 10 to each element.
2.Given a NumPy array 'marks = np.array([67, 82, 45, 91, 38, 74])', how can you set all values below 40 to 40?
- AIterate with a for loop and set values conditionally.
- BUse 'np.where(marks < 40, 40, marks)'.
- CUse 'marks[marks < 40] = 40'.
- DApply a lambda function to each element.
3.A developer notices their code using a Python list is slow. They switch to NumPy arrays and see a speedup. Why?
- ANumPy arrays automatically parallelize operations across cores.
- BNumPy arrays are executed on the GPU by default.
- CNumPy arrays are stored in a compressed format.
- DNumPy arrays use less memory and support vectorized operations.
4.What will be the output of 'np.where(scores > 75, "good", "work")' if 'scores = np.array([[80, 90, 70], [60, 85, 95]])'?
- A[['good', 'work', 'work'], ['work', 'good', 'good']]
- B[['good', 'work', 'good'], ['good', 'good', 'work']]
- C[['good', 'good', 'work'], ['work', 'work', 'good']]
- D[['good', 'good', 'work'], ['work', 'good', 'good']]
5.A NumPy array 'a = np.arange(10)' is sliced with 'view = a[2:5]'. What happens if 'view[0] = 99'?
- AOnly the 'view' array is modified, not the original.
- BA copy of 'a' is modified, leaving 'a' unchanged.
- CAn error occurs because 'view' is a read-only view.
- DThe original array 'a' is modified at index 2.
6.A user wants to normalize each column of a 2D NumPy array to the range 0-1. Which method is correct?
- ASubtract the mean and divide by the standard deviation.
- BTranspose the array, normalize rows, then transpose back.
- CUse '(scores - scores.min()) / (scores.max() - scores.min())'.
- DUse '(scores - scores.min(axis=0)) / (scores.max(axis=0) - scores.min(axis=0))'.
7.A developer mistakenly believes that reshaping a NumPy array always creates a new copy. What is the truth?
- AReshaping modifies the original array in place.
- BReshaping creates a view if possible, not a copy.
- CReshaping results in a shallow copy, not a view.
- DReshaping always creates a deep copy of the data.
8.Why might a NumPy operation like 'big ** 2' be significantly faster than a Python loop for squaring elements?
- ANumPy uses vectorized operations at C speed.
- BNumPy operations are automatically parallelized.
- CNumPy stores data in a compressed format.
- DNumPy operations are executed on the GPU.