← 所有公开测验
开始测验(游客或账户)
Python — NumPy: arrays & vectorized computation
作者 Jude · 23/05/2026 · 120 次浏览 · 次分享
Creating ndarrays, dtypes, shape, broadcasting, advanced indexing, aggregations, and performance. 20 questions.
20
题目
🇬🇧 English
语言
中等
难度
测验题目
-
1. What is the main difference between a Python
listand anumpy.ndarray?- None
ndarrayis typed, contiguous in memory, and supports vectorized ops; lists are heterogeneous and slower- Lists are faster for numeric work
ndarrayis a subclass oflist
-
2. What is
a.shape?import numpy as np a = np.array([[1, 2, 3], [4, 5, 6]]) print(a.shape)- (2, 3)
- (3, 2)
- (6,)
- (2, 3, 1)
-
3. What does this print?
a = np.arange(6).reshape(2, 3) print(a.sum(axis=0))- [3 5 7]
- [3 12]
- [0 1 2 3 4 5]
- 15
-
4. What is the broadcast output shape?
a = np.array([[1], [2], [3]]) # shape (3, 1) b = np.array([10, 20, 30]) # shape (3,) print((a + b).shape)- (3,)
- (3, 3)
- (1, 3)
- ValueError
-
5. Default dtype of
np.array([1, 2, 3])on a 64-bit OS?- int8
- int32
- int64
- float64
-
6. What is
aafter this?a = np.array([1, 2, 3, 4, 5]) b = a[1:4] b[0] = 99 print(a)- [1 2 3 4 5]
- [1 99 3 4 5]
- [1 99 99 99 5]
- Error
-
7. What does boolean indexing produce?
a = np.array([10, 20, 30, 40]) print(a[a > 15])- [10 20 30 40]
- [20 30 40]
- [False True True True]
- Error
-
8. Difference between
np.dot(a, b)anda * bfor matrices?- None
np.dotis matrix multiplication;*is element-wise (Hadamard) product*is fasternp.dotis deprecated
-
9. What does this return?
a = np.zeros((2, 3), dtype=np.int32) print(a.dtype, a.nbytes)- int32 12
- int32 24
- int64 24
- float64 48
-
10. Which generates 100 evenly spaced values from 0 to 1 inclusive?
np.arange(0, 1, 100)np.linspace(0, 1, 100)np.range(0, 1, 100)np.logspace(0, 1, 100)
-
11. What does this print?
a = np.array([1, 2, np.nan, 4]) print(a.sum(), np.nansum(a))- 7.0 7.0
- nan 7.0
- 7.0 nan
- Error
-
12. How do you reshape (12,) to (3, 4) without copying when possible?
a.reshape(3, 4)a.resize(3, 4)onlynp.copy(a).reshape(3, 4)- Impossible without copy
-
13. What is the output?
a = np.array([[1, 2], [3, 4]]) print(a.T)- [[1 2] [3 4]]
- [[1 3] [2 4]]
- [[4 3] [2 1]]
- Error
-
14. Asymptotic complexity of standard
(n, n) @ (n, n)?- O(n)
- O(n²)
- O(n³)
- O(n log n)
-
15. What does this print?
rng = np.random.default_rng(42) print(rng.integers(0, 10, size=3))- Three reproducible ints in [0, 10)
- Three floats between 0 and 10
- Three reproducible ints in [0, 10] (inclusive)
- Error —
default_rngdoes not exist
-
16. Which expression sets negatives to 0?
a[a < 0] = 0a.replace(< 0, 0)a.zero_negatives()np.abs(a)
-
17. Difference between
a.flatten()anda.ravel()?- None
flattenalways copies;ravelreturns a view if possibleravelis slowerflattenonly works on 1D
-
18. What is
r?a = np.array([[1, 2, 3], [4, 5, 6]]) r = a[:, [2, 0]] print(r)- [[3 1] [6 4]]
- [[1 3] [4 6]]
- [[2 0] [5 3]]
- Error
-
19. Best way to read/write arrays in NumPy's native binary format?
np.save/np.loadpickle.dump/pickle.loadjson.dumpopen(..., "wb")+str(a)
-
20. Why vectorize instead of looping in Python for elementwise math?
- No reason
- Vectorized ops run in C/SIMD instead of the Python interpreter, often 10×–100× faster
- Python loops are faster in NumPy
- Vectorizing uses more memory and is always slower