Python NumPy Arrays Quiz
A 35-question quiz covering NumPy array creation, shapes, indexing, slicing, broadcasting, and aggregation.
Question 1
Why are NumPy arrays generally preferred over standard Python lists for scientific computing?
Question 2
You need to initialize a neural network weight matrix of size 3x3 with all zeros. Which command achieves this?
import numpy as np
# Create 3x3 zero matrix
Question 3
What is the key difference between `np.arange()` and `np.linspace()`?
Question 4
Consider the following code snippet creating an array. What will be the resulting data type?
import numpy as np
arr = np.array([1, 2, 3.5, 4])
print(arr.dtype)
Question 5
How would you create a 4x4 Identity matrix (1s on the diagonal, 0s elsewhere)?
Question 6
What happens if you try to create a NumPy array with a mix of strings and numbers?
arr = np.array([1, "2", 3])
Question 7
Which attribute would you check to determine the dimensions (rows, columns, etc.) of an array?
Question 8
You have an array with 12 elements. You want to reshape it into a matrix with 3 rows and an automatically calculated number of columns. How do you do this?
arr = np.arange(12)
# Reshape command
Question 9
What is the difference between `arr.flatten()` and `arr.ravel()`?
Question 10
Given a 3D array of shape `(2, 3, 4)`, what does `.ndim` return?
Question 11
How do you transpose a 2D matrix `arr` (swap rows and columns)?
Question 12
If you reshape an array `arr` of shape `(4, 4)` to `(2, 8)`, does the underlying data change?
Question 13
Consider the array `M`. How do you access the element in the second row and third column?
M = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Question 14
You want to set all negative values in an array `data` to 0. Which operation uses Boolean Masking correctly?
data = np.array([-1, 2, -3, 4, 5])
Question 15
What is the result of this 'Fancy Indexing' operation?
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 3, 4]
print(arr[indices])
Question 16
How does NumPy handle indexing with a tuple of integers vs a list of integers?
Question 17
What happens if you assign a float value to an integer array element?
arr = np.array([1, 2, 3], dtype='int32')
arr[0] = 10.9
print(arr)
Question 18
You have a 2D array. You want to select the first and third rows, and for those rows, only the second column. Which syntax is correct?
arr = np.array([[1, 2], [3, 4], [5, 6]])
# Target: [2, 6]
Question 19
What does the slice `arr[::-1]` do?
Question 20
Given a 2D array `grid`, how do you select the entire second column?
grid = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Question 21
What is the key difference between slicing and fancy indexing regarding memory?
Question 22
What happens when you modify a slice of an array?
arr = np.array([0, 1, 2, 3, 4])
sub = arr[0:2]
sub[0] = 99
print(arr[0])
Question 23
How do you select the last 3 elements of an array?
Question 24
Consider the array `img` of shape `(100, 100, 3)`. What does `img[:50, :50, :]` select?
Question 25
What is 'Broadcasting' in NumPy?
Question 26
Which of the following shapes are compatible for broadcasting?
Question 27
What is the result of adding a scalar to an array?
arr = np.array([1, 2, 3])
print(arr + 10)
Question 28
Predict the shape of the result: `A` is `(3, 1)` and `B` is `(1, 3)`.
A = np.zeros((3, 1))
B = np.zeros((1, 3))
C = A + B
Question 29
Why is broadcasting efficient?
Question 30
You want to subtract the mean of each column from a 2D array `X` of shape `(100, 5)`. The mean vector `mu` has shape `(5,)`. Does `X - mu` work?
Question 31
What does `np.sum(arr, axis=0)` do on a 2D array?
Question 32
How do you find the *index* of the maximum value in an array?
arr = np.array([10, 5, 99, 2])
# Wanted: 2 (index of 99)
Question 33
Why is `np.sum()` faster than Python's built-in `sum()` on NumPy arrays?
Question 34
What is the result of `arr.mean()` if `arr` contains integers?
arr = np.array([1, 2])
print(arr.mean())
Question 35
You want to calculate the cumulative sum of an array. Which function should you use?
arr = np.array([1, 2, 3])
# Result: [1, 3, 6]
