Unit 7: Operations on NumPy arrays - Practice Quiz
1 Which NumPy method changes the shape of an array without changing its data?
2 Which attribute returns the dimensions of a NumPy array?
3
What does the flatten() method return?
4 Which NumPy function joins arrays along an existing axis?
5
What does arr.T represent for a two-dimensional NumPy array?
6 Which NumPy function divides an array into multiple subarrays?
7
What is the shape of np.array([1, 2, 3, 4]).reshape(2, 2)?
8 What is broadcasting in NumPy?
9
What happens when the scalar 5 is added to a NumPy array?
10 Which pair of shapes is compatible for broadcasting?
11 When comparing dimensions during broadcasting, which value is compatible with any dimension size?
12
What is the result of np.array([1, 2, 3]) + 2?
13 For broadcasting, NumPy compares array shapes starting from which side?
14 What is a key benefit of NumPy broadcasting?
15 Which operator performs element-wise addition on NumPy arrays?
16
What is the result of np.array([2, 4]) * np.array([3, 5])?
17 Which operator performs element-wise exponentiation in NumPy?
18 Which operator performs matrix multiplication between two suitable NumPy arrays?
19
What does the % operator calculate element-wise?
20
What is the result of np.array([5, 8]) > np.array([3, 10])?
21
What is printed by the following code?
a = np.arange(6)
b = a.reshape(2, 3)
b[0, 0] = 99
print(a[0])
0
6
99
IndexError
22
Given a = np.arange(24).reshape(2, 3, 4) and b = np.transpose(a, (1, 0, 2)), what are b.shape and b[2, 1, 3]?
(4, 2, 3) and 19
(3, 4, 2) and 19
(2, 4, 3) and 23
(3, 2, 4) and 23
23
What is the result of np.concatenate((x, y), axis=1) when x = np.ones((2, 2), dtype=int) and y = np.zeros((2, 1), dtype=int)?
[[1, 0, 1], [1, 0, 1]]
[[1, 1, 0], [1, 1, 0]]
[[1, 1], [1, 1], [0, 0]]
[[1, 1, 1], [0, 0, 0]]
24
Given x = np.array([1, 2]) and y = np.array([3, 4]), what does np.stack((x, y), axis=1) produce?
[[1], [2], [3], [4]]
[[1, 2], [3, 4]]
[1, 2, 3, 4]
[[1, 3], [2, 4]]
25
What are the lengths of the arrays returned by np.array_split(np.arange(10), 3)?
[4, 4, 2]
[3, 3, 4]
[3, 3, 3]
[4, 3, 3]
26
Given a = np.array([[1, 2, 3], [4, 5, 6]]), what does np.flip(a, axis=1) return?
[[1, 4], [2, 5], [3, 6]]
[[6, 5, 4], [3, 2, 1]]
[[4, 5, 6], [1, 2, 3]]
[[3, 2, 1], [6, 5, 4]]
27
After running a = np.array([[1, 2], [3, 4]]), b = a.flatten(), and b[0] = 9, what is a?
[[9, 2], [3, 4]]
[[1, 2], [3, 4]]
[[1, 9], [3, 4]]
[[9, 2], [9, 4]]
28
What is the shape of the result when arrays with shapes (3, 1) and (1, 4) are added?
(4, 3)
(1, 4)
(3, 1)
(3, 4)
29
What is the result of np.array([[1, 2, 3], [4, 5, 6]]) + np.array([10, 20, 30])?
[[31, 32, 33], [34, 35, 36]]
[[11, 22, 33], [14, 25, 36]]
[[11, 12, 13], [24, 25, 26]]
[[10, 40, 90], [40, 100, 180]]
30
An array a has shape (2, 3, 4) and an array b has shape (3, 1). What is the shape of a + b?
(2, 3, 1)
(2, 3, 4)
(3, 3, 4)
(2, 1, 4)
31
What happens when an array of shape (4, 3) is added to an array of shape (4,)?
ValueError is raised
(4, 4) is created
(3, 4) is created
(4, 3) is created
32
For a = np.array([[1., 3.], [2., 6.]]), which expression subtracts each row's mean from that row using broadcasting?
a - a.mean(axis=1)
a - a.mean(axis=1, keepdims=True)
a - a.mean()
a - a.mean(axis=0, keepdims=True)
33
If a = np.array([1, 2, 3]) and b = np.array([10, 20]), what is the shape of a[:, np.newaxis] * b?
(2, 3)
(1, 2)
(3, 1)
(3, 2)
34
What is the final value of a after a = np.zeros((2, 3), dtype=int) followed by a[:] = np.array([1, 2, 3])?
[[1, 2, 3], [0, 0, 0]]
[[1, 1, 1], [2, 2, 2]]
[[1, 2, 0], [3, 0, 0]]
[[1, 2, 3], [1, 2, 3]]
35
Given a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what does a * b return?
[4, 10, 18]
[4, 5, 6]
[5, 7, 9]
32
36
What is the result of np.array([[1, 2], [3, 4]]) @ np.array([5, 6])?
[11, 25]
[23, 34]
[17, 39]
[5, 12]
37
For a = np.array([0, 1, 2, 3, 4]), what does (a > 1) & (a < 4) return?
[False, False, True, True, True]
[True, True, False, False, True]
[False, False, True, True, False]
[False, True, True, True, False]
38
What is the result of np.array([-7, 7]) // 3?
[-3, 2]
[-2, 3]
[-3, 3]
[-2, 2]
39
What does np.array([5, 6, 7]) ^ np.array([3, 3, 3]) produce?
[7, 7, 7]
[2, 3, 4]
[1, 2, 3]
[6, 5, 4]
40
For a = np.array([1, 2, 3, 4]), what does (a % 2 == 0) | (a > 3) return?
[False, True, True, True]
[True, False, True, False]
[False, True, False, True]
[False, False, False, True]
41
What is printed by the following code?
a = np.arange(12).reshape(3, 4)
b = a.T
c = b.reshape(2, 6)
c[0, 1] = -1
print(a[1, 0], np.shares_memory(a, c))
4 False
-1 True
-1 False
4 True
42
What is the value of b[-1] after this code?
a = np.array([1, 2, 3, 4])
b = np.resize(a, (3, 3))
array([4, 0, 0])
array([1, 2, 3])
array([3, 4, 1])
array([4, 1, 2])
43
If x.shape is (2, 3, 4, 5), what is the shape of np.moveaxis(x, (0, 3), (2, 0))?
(3, 5, 2, 4)
(5, 4, 2, 3)
(5, 3, 2, 4)
(5, 2, 3, 4)
44
Which sequence of lists is produced by [part.tolist() for part in np.array_split(np.arange(10), 3)]?
[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[0, 1, 2], [3, 4, 5, 6], [7, 8, 9]]
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
[[0, 1, 2], [3, 4, 5], [6, 7, 8, 9]]
45
What is the final value of a?
a = np.zeros(4, dtype=int)
idx = np.array([0, 0, 2])
a[idx] += 1
array([2, 0, 1, 0])
array([1, 1, 1, 0])
array([1, 0, 1, 0])
array([2, 0, 2, 0])
46
What happens when np.empty((0, 3)).reshape(-1, 0) is evaluated?
(0, 0) is returned
(0, 3) is returned
ValueError is raised
(1, 0) is returned
47
Given two arrays a and b, each with shape (2, 3), what is the shape of np.stack((a, b), axis=-1)?
(4, 3)
(2, 6)
(2, 3, 2)
(2, 2, 3)
48
What is the result shape of adding arrays with shapes (2, 1, 3, 1) and (1, 4, 1, 5)?
(1, 4, 3, 5)
(2, 4, 3, 5)
(2, 4, 1, 5)
(2, 1, 3, 5)
49 Which pair of shapes cannot be broadcast together under NumPy's standard broadcasting rules?
(2, 3, 1) and (3, 4, 1)
(2, 1, 4) and (3, 4)
(3, 2, 1) and (2, 4)
(3, 1, 5) and (1, 4, 1)
50
What is the value of a[1, :, 2] after this code?
a = np.zeros((2, 3, 4), dtype=int)
v = np.arange(3).reshape(3, 1)
a[:] = v
array([1, 1, 1])
array([0, 1, 2])
array([2, 2, 2])
array([0, 0, 0])
51
What happens in the following code?
a = np.array([1, 2, 3])
b = np.broadcast_to(a, (4, 3))
b[0, 0] = 9
a[0] and the first column become equal to 9
ValueError occurs because b is read-only
b[0, 0] becomes equal to 9
b becomes equal to 9
52
What is the shape of the result when arrays with shapes (0, 3, 1) and (1, 1, 4) are added?
ValueError
(0, 1, 4)
(1, 3, 4)
(0, 3, 4)
53
What does the following expression return?
x = np.array([[1], [2]])
y = np.array([10, 20, 30])
c = np.array([[True, False, True], [False, True, False]])
np.where(c, x, y)
array([[1, 20, 1], [10, 2, 30]])
array([[1, 10, 1], [20, 2, 20]])
array([[10, 1, 30], [2, 20, 2]])
array([[1, 20, 1], [30, 2, 10]])
54
For matrix multiplication, A has shape (2, 1, 3, 4) and B has shape (5, 4, 6). What is the shape of A @ B?
(2, 1, 3, 6)
(2, 5, 3, 6)
(5, 2, 3, 6)
(2, 5, 4, 6)
55
What are the values of a // b and a % b?
a = np.array([-7, 7])
b = np.array([3, -3])
array([-2, -2]) and array([-1, 1])
array([-3, -3]) and array([2, -2])
array([-2, -3]) and array([-1, -2])
array([-3, -2]) and array([2, 1])
56
Assuming standard NumPy fixed-width integer arithmetic, what is produced by this expression?
np.array([1, 2, 3], dtype=np.uint8) - np.array([2], dtype=np.uint8)
array([255, 1, 2], dtype=uint8)
array([0, 0, 1], dtype=uint8)
array([255, 0, 1], dtype=uint8)
array([-1, 0, 1], dtype=int16)
57
What happens when the following expression is evaluated?
a = np.array([0, 2, 4])
a > 1 & a < 4
array([False, True, False])
ValueError about ambiguous truth values is raised
array([False, True, True])
TypeError about unsupported operands is raised
58
What values are printed by this code?
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
print(A @ b, b @ A)
[17 23] [34 39]
[15 24] [20 36]
[23 34] [17 39]
[17 39] [23 34]
59
What happens when np.array([2, 4], dtype=int) ** -1 is evaluated?
array([0, 0]) is returned
array([0.5, 0.25]) is returned
array([1, 1]) is returned
ValueError is raised
60
What is the result of a ^ b & a for the arrays below?
a = np.array([True, False, True])
b = np.array([True, True, False])
array([False, True, True])
array([False, False, True])
array([True, True, False])
array([True, False, False])
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 →