Unit 4: Array Operations using NumPy - Practice Quiz
1
Which Python library provides the ndarray object used for numerical arrays?
2 What is a key difference between a NumPy array and a Python list?
3 Which function is used to create a NumPy array from a Python list?
np.create()
np.array()
np.list()
np.make()
4 Compared to Python lists, NumPy arrays are generally preferred for numerical computation because they are:
5 Which attribute is used to check the data type of elements in a NumPy array?
.dtype
.type
.datatype
.kind
6
What does the NumPy data type int64 represent?
7 Which method changes the data type of an existing NumPy array?
.convert()
.setdtype()
.astype()
.retype()
8
If an array is created as np.array([1.0, 2.0, 3.0]), its default data type will be:
bool
str
int32
float64
9
Given a = np.array([1, 2, 3]), what is the result of a + 2?
[3, 4, 5]
[2, 4, 6]
[3, 2, 3]
[1, 2, 3, 2]
10 Which attribute returns the shape (dimensions) of a NumPy array?
.dim
.size
.shape
.length
11
Given a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what is a * b?
[4, 5, 6]
[1, 2, 3]
[5, 7, 9]
[4, 10, 18]
12 Which function reshapes an array without changing its data?
resize()
reshape()
restack()
reform()
13
What does np.arange(0, 5) produce?
[0, 1, 2, 3, 4, 5]
[0, 5]
[1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
14 Which NumPy function computes the average of array elements?
np.center()
np.mean()
np.middle()
np.avg()
15
What does np.max() return for a NumPy array?
16 Which function computes the standard deviation of a NumPy array?
np.var()
np.dev()
np.std()
np.spread()
17
Given a = np.array([2, 4, 6]), what does np.sum(a) return?
12
6
4
3
18 What does broadcasting in NumPy allow you to do?
19 When you add a scalar to a NumPy array, broadcasting causes the scalar to be:
20 For broadcasting to work between two arrays, their dimensions must be:
21
Given import numpy as np, what is the output of the following code?
python
lst = [1, 2, 3]
arr = np.array([1, 2, 3])
print(lst 2, arr 2)
[2, 4, 6] [1 2 3 1 2 3]
[1, 2, 3, 1, 2, 3] [2 4 6]
[1, 2, 3, 1, 2, 3] [1 2 3 1 2 3]
[2, 4, 6] [2 4 6]
22 Which statement best explains why NumPy arrays are generally faster than Python lists for numerical computation?
23
What will be the dtype of the array created below?
python
arr = np.array([1, 2, 3.0, 4])
float64
complex128
object
int64
24
What is the output of the following code?
python
arr = np.array([1, 2, 3], dtype=np.int8)
arr[0] = 300
print(arr[0])
127
300
44
25
Which code correctly converts a float array a into an integer array without changing the original?
b = a.astype(np.int32)
b = a.convert('int32')
b = a.dtype(np.int32)
b = int(a)
26
Given arr = np.arange(12).reshape(3, 4), what does arr[:, 1] return?
[1 5 9]
[0 1 2 3]
[4 5 6 7]
[1 2 3 4]
27
What is the result of the following code?
python
a = np.array([[1, 2], [3, 4]])
print(a.sum(axis=0))
[3 7]
[4 6]
[6 4]
10
28
Given arr = np.array([10, 20, 30, 40, 50]), what does arr[::-2] return?
[50 40 30 20 10]
[50 30 10]
[10 30 50]
[40 20]
29
What is the difference between np.dot(a, b) and a * b for two 2D arrays of the same shape?
* is faster
np.dot performs element-wise multiplication while * performs matrix multiplication
np.dot performs matrix multiplication while * performs element-wise multiplication
30
What is the output of the following code?
python
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr % 2 == 0])
[2 4]
[True False True False True]
[0 2 0 4 0]
[1 3 5]
31
Given arr = np.array([2, 4, 6, 8]), what does np.std(arr) compute and its approximate value?
32
For a = np.array([[1, 2], [3, 4]]), what does np.mean(a, axis=1) return?
2.5
[4. 6.]
[2. 3.]
[1.5 3.5]
33 Which function returns the index of the maximum value in an array rather than the value itself?
np.amax
np.maximum
np.argmax
np.max
34
What is the output of the following code?
python
arr = np.array([3, 1, 4, 1, 5, 9, 2])
print(np.median(arr))
4.0
3.5
1.0
3.0
35
Given arr = np.array([10, 20, 30, 40]), what does np.cumsum(arr) return?
100
[100 90 70 40]
[10 30 60 100]
[10 20 30 40]
36
What is the result of the following code?
python
a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30])
print((a + b).shape)
(3, 3)
(3,)
(3, 1)
37 Which pair of array shapes CANNOT be broadcast together?
(4, 3) and (2,)
(2, 3) and (1, 3)
(5, 1) and (1, 6)
(3, 4) and (4,)
38
What is the output of the following code?
python
a = np.array([1, 2, 3])
print(a + 5)
[5 5 5]
[1 2 3 5]
[6 7 8]
39
Given a of shape (2, 3) and b of shape (3,), what is the shape of a * b?
(2, 3)
(3,)
(2,)
40
What is the output of the following code?
python
arr = np.array([1, 2, 3])
arr2 = arr
arr2[0] = 99
print(arr[0])
0
99
1
41
Consider the following code:
import numpy as np
lst = [1, 2, 3]
arr = np.array([1, 2, 3])
print(lst * 2)
print(arr * 2)
What is the output?
[2, 4, 6] and [1 2 3 1 2 3]
[1, 2, 3, 1, 2, 3] and [2 4 6]
[1, 2, 3, 1, 2, 3] and [1 2 3 1 2 3]
[2, 4, 6] and [2 4 6]
42 A NumPy array stores integers while an equivalent Python list stores the same values. Why does the NumPy array typically consume far less memory?
43
What does the following code print?
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int8)
arr[0] = 130
print(arr[0])
-126
127
2
130
44
Given arr = np.array([1, 2, 3]), which operation returns a new array of type float64 without modifying arr?
arr.dtype = np.float64
arr.reshape(np.float64)
arr.view(np.float64)
arr.astype(np.float64)
45
What is the resulting dtype of np.array([True, 2, 3.5])?
bool
int64
object
float64
46
What does the following print?
import numpy as np
a = np.arange(6).reshape(2, 3)
b = a.T
b[0, 0] = 99
print(a[0, 0])
0
5
99
47
Given a = np.array([10, 20, 30, 40]), what does a[::-1][1:3] return?
array([30, 20])
array([20, 30])
array([30, 40])
array([40, 30])
48
Consider:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
mask = a % 2 == 0
a[mask] = 0
print(a)
What is the output?
[1 2 3 4 5]
[0 0 0 0 0]
[1 0 3 0 5]
[0 2 0 4 0]
49
What is the result of np.arange(12).reshape(3, 4)[:, 1::2]?
[[1 2], [5 6], [9 10]]
[[2 3], [6 7], [10 11]]
[[0 2], [4 6], [8 10]]
[[1 3], [5 7], [9 11]]
50
Which statement about np.reshape(a, (-1, 3)) is correct when a has 12 elements?
-1 is treated as , producing an empty array
-1 reverses the array before reshaping
-1 is not a valid dimension
-1 is inferred as , producing a array
51
For a = np.array([[1, 2], [3, 4]]), what does a.mean(axis=0) return?
array([2., 3.])
array([1.5, 3.5])
2.5
array([1.5, 2.5, 3.5])
52
What does np.std(np.array([2, 4, 4, 4, 5, 5, 7, 9])) return (population standard deviation)?
4.0
5.0
2.14
2.0
53
Given an array containing a np.nan, which function ignores the NaN when computing the mean?
np.mean
np.nanmean
np.average
np.mean with axis=None
54
What is the difference between a.var() and a.var(ddof=1) for a sample array?
ddof=1 computes standard deviation instead of variance
ddof=1 divides by (sample variance) instead of (population variance)
ddof=1 divides by instead of
55
For a = np.array([[1, 5], [3, 2]]), what does np.argmax(a) return?
5
1
(0, 1)
3
56
Given a of shape (3, 1) and b of shape (1, 4), what is the shape of a + b?
(1, 4)
(3, 1)
(3, 4)
57 Which pair of shapes CANNOT be broadcast together?
(3, 4) and (2, 4)
(5, 3, 4) and (3, 4)
(3, 4) and (4,)
(3, 1) and (1, 4)
58
What does the following produce?
import numpy as np
a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30])
print((a + b).shape)
(3, 1)
(9,)
(1, 3)
(3, 3)
59
To subtract the per-column mean from each element of a (100, 5) array X, which expression works via broadcasting?
X - X.mean(axis=1)
X - X.mean(axis=1).reshape(-1, 1) giving column-mean subtraction
X - X.mean(axis=0)
X - X.mean()
60
What is the result of the following?
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[0], [1], [2]])
print(a * b)
[[1 2 3], [1 2 3], [1 2 3]]
[[0 2 6]]
[[0 0 0], [1 2 3], [2 4 6]]
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →