Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 7 MCQsOperations on NumPy arrays
ECAP776 ·
60 questions. Pick an option to check it.
Which NumPy method changes the shape of an array without changing its data?
Not quite. Try another option.
Answer: reshape()
The reshape() method gives an array a new shape while preserving its elements.
Question 2 of 60 · Array manipulation · Easy
Which attribute returns the dimensions of a NumPy array?
Not quite. Try another option.
Answer: shape
The shape attribute returns a tuple containing the size of each array dimension.
Question 3 of 60 · Array manipulation · Easy
What does the flatten() method return?
Not quite. Try another option.
Answer: A one-dimensional copy
The flatten() method converts the array into a new one-dimensional copy.
Question 4 of 60 · Array manipulation · Easy
Which NumPy function joins arrays along an existing axis?
Not quite. Try another option.
Answer: np.concatenate()
np.concatenate() joins two or more arrays along an existing axis.
Question 5 of 60 · Array manipulation · Easy
What does arr.T represent for a two-dimensional NumPy array?
Not quite. Try another option.
Answer: The transposed array
arr.T exchanges the rows and columns of a two-dimensional array.
Question 6 of 60 · Array manipulation · Easy
Which NumPy function divides an array into multiple subarrays?
Not quite. Try another option.
Answer: np.split()
np.split() separates an array into multiple subarrays.
Question 7 of 60 · Array manipulation · Easy
What is the shape of np.array([1, 2, 3, 4]).reshape(2, 2)?
Not quite. Try another option.
Answer: (2, 2)
Reshaping four elements with (2, 2) creates two rows and two columns.
Question 8 of 60 · Broadcasting · Easy
What is broadcasting in NumPy?
Not quite. Try another option.
Answer: Operating on compatible shapes
Broadcasting allows NumPy to perform operations on arrays whose shapes are compatible.
Question 9 of 60 · Broadcasting · Easy
What happens when the scalar 5 is added to a NumPy array?
Not quite. Try another option.
Answer: It is added to every element
NumPy broadcasts the scalar across the array and adds it to each element.
Question 10 of 60 · Broadcasting · Easy
Which pair of shapes is compatible for broadcasting?
Not quite. Try another option.
Answer: (3, 4) and (4,)
The trailing dimensions are both 4, so the shapes are compatible.
Question 11 of 60 · Broadcasting · Easy
When comparing dimensions during broadcasting, which value is compatible with any dimension size?
Not quite. Try another option.
Answer: 1
A dimension of size 1 can be expanded to match the corresponding dimension.
Question 12 of 60 · Broadcasting · Easy
What is the result of np.array([1, 2, 3]) + 2?
Not quite. Try another option.
Answer: [3, 4, 5]
The scalar 2 is broadcast and added to every array element.
Question 13 of 60 · Broadcasting · Easy
For broadcasting, NumPy compares array shapes starting from which side?
Not quite. Try another option.
Answer: The rightmost side
NumPy compares dimensions from right to left when applying broadcasting rules.
Question 14 of 60 · Broadcasting · Easy
What is a key benefit of NumPy broadcasting?
Not quite. Try another option.
Answer: It avoids many explicit loops
Broadcasting supports vectorized operations without requiring many explicit Python loops.
Question 15 of 60 · Binary operators · Easy
Which operator performs element-wise addition on NumPy arrays?
Not quite. Try another option.
Answer: +
The + operator adds corresponding elements of compatible NumPy arrays.
Question 16 of 60 · Binary operators · Easy
What is the result of np.array([2, 4]) * np.array([3, 5])?
Not quite. Try another option.
Answer: [6, 20]
The * operator multiplies corresponding elements, giving and .
Question 17 of 60 · Binary operators · Easy
Which operator performs element-wise exponentiation in NumPy?
Not quite. Try another option.
Answer: **
The ** operator raises each element to the specified power.
Question 18 of 60 · Binary operators · Easy
Which operator performs matrix multiplication between two suitable NumPy arrays?
Not quite. Try another option.
Answer: @
The @ operator performs matrix multiplication, while * performs element-wise multiplication.
Question 19 of 60 · Binary operators · Easy
What does the % operator calculate element-wise?
Not quite. Try another option.
Answer: Remainders
The % operator returns the remainder for each element-wise division.
Question 20 of 60 · Binary operators · Easy
What is the result of np.array([5, 8]) > np.array([3, 10])?
Not quite. Try another option.
Answer: [True, False]
The comparison is element-wise: is true, while is false.
Question 21 of 60 · Array manipulation · Medium
What is printed by the following code?
a = np.arange(6)
b = a.reshape(2, 3)
b[0, 0] = 99
print(a[0])
Not quite. Try another option.
Answer: 99
reshape returns a view when possible. Here, b shares data with a, so changing b[0, 0] changes a[0].
Question 22 of 60 · Array manipulation · Medium
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]?
Not quite. Try another option.
Answer: (3, 2, 4) and 23
The axis order (1, 0, 2) changes the shape to (3, 2, 4). Thus, b[2, 1, 3] refers to a[1, 2, 3], which is 23.
Question 23 of 60 · Array manipulation · Medium
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)?
Not quite. Try another option.
Answer: [[1, 1, 0], [1, 1, 0]]
Concatenation along axis=1 joins columns. The single zero column is appended to the two columns of ones.
Question 24 of 60 · Array manipulation · Medium
Given x = np.array([1, 2]) and y = np.array([3, 4]), what does np.stack((x, y), axis=1) produce?
Not quite. Try another option.
Answer: [[1, 3], [2, 4]]
stack creates a new axis. With axis=1, corresponding elements from x and y become rows.
Question 25 of 60 · Array manipulation · Medium
What are the lengths of the arrays returned by np.array_split(np.arange(10), 3)?
Not quite. Try another option.
Answer: [4, 3, 3]
array_split allows unequal sections and places the extra element in the earliest section, producing lengths 4, 3, and 3.
Question 26 of 60 · Array manipulation · Medium
Given a = np.array([[1, 2, 3], [4, 5, 6]]), what does np.flip(a, axis=1) return?
Not quite. Try another option.
Answer: [[3, 2, 1], [6, 5, 4]]
axis=1 reverses the order of columns within each row while leaving the row order unchanged.
Question 27 of 60 · Array manipulation · Medium
After running a = np.array([[1, 2], [3, 4]]), b = a.flatten(), and b[0] = 9, what is a?
Not quite. Try another option.
Answer: [[1, 2], [3, 4]]
flatten() returns a copy, so modifying b does not alter the original array a.
Question 28 of 60 · Broadcasting · Medium
What is the shape of the result when arrays with shapes (3, 1) and (1, 4) are added?
Not quite. Try another option.
Answer: (3, 4)
The first array is broadcast across four columns, while the second is broadcast across three rows, giving shape (3, 4).
Question 29 of 60 · Broadcasting · Medium
What is the result of np.array([[1, 2, 3], [4, 5, 6]]) + np.array([10, 20, 30])?
Not quite. Try another option.
Answer: [[11, 22, 33], [14, 25, 36]]
The one-dimensional array is broadcast across both rows, so its three values are added column by column.
Question 30 of 60 · Broadcasting · Medium
An array a has shape (2, 3, 4) and an array b has shape (3, 1). What is the shape of a + b?
Not quite. Try another option.
Answer: (2, 3, 4)
From the right, (3, 1) aligns with (3, 4). The size 1 expands to 4, and b is also applied across the leading size 2.
Question 31 of 60 · Broadcasting · Medium
What happens when an array of shape (4, 3) is added to an array of shape (4,)?
Not quite. Try another option.
Answer: ValueError is raised
Trailing dimensions 3 and 4 are neither equal nor 1, so the shapes are not broadcast-compatible.
Question 32 of 60 · Broadcasting · Medium
For a = np.array([[1., 3.], [2., 6.]]), which expression subtracts each row's mean from that row using broadcasting?
Not quite. Try another option.
Answer: a - a.mean(axis=1, keepdims=True)
Using axis=1 computes one mean per row, and keepdims=True gives shape (2, 1), which broadcasts across the columns.
Question 33 of 60 · Broadcasting · Medium
If a = np.array([1, 2, 3]) and b = np.array([10, 20]), what is the shape of a[:, np.newaxis] * b?
Not quite. Try another option.
Answer: (3, 2)
a[:, np.newaxis] has shape (3, 1), while b behaves as shape (1, 2). Broadcasting produces shape (3, 2).
Question 34 of 60 · Broadcasting · Medium
What is the final value of a after a = np.zeros((2, 3), dtype=int) followed by a[:] = np.array([1, 2, 3])?
Not quite. Try another option.
Answer: [[1, 2, 3], [1, 2, 3]]
The shape (3,) array is broadcast across the two rows of the (2, 3) destination.
Question 35 of 60 · Binary operators · Medium
Given a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what does a * b return?
Not quite. Try another option.
Answer: [4, 10, 18]
For NumPy arrays, * performs element-wise multiplication rather than a dot product.
Question 36 of 60 · Binary operators · Medium
What is the result of np.array([[1, 2], [3, 4]]) @ np.array([5, 6])?
Not quite. Try another option.
Answer: [17, 39]
The @ operator performs matrix-vector multiplication: the row products are and .
Question 37 of 60 · Binary operators · Medium
For a = np.array([0, 1, 2, 3, 4]), what does (a > 1) & (a < 4) return?
Not quite. Try another option.
Answer: [False, False, True, True, False]
The & operator combines the two element-wise conditions. Only 2 and 3 are greater than 1 and less than 4.
Question 38 of 60 · Binary operators · Medium
What is the result of np.array([-7, 7]) // 3?
Not quite. Try another option.
Answer: [-3, 2]
The // operator performs floor division. Since is floored toward negative infinity, it becomes -3, while becomes 2.
Question 39 of 60 · Binary operators · Medium
What does np.array([5, 6, 7]) ^ np.array([3, 3, 3]) produce?
Not quite. Try another option.
Answer: [6, 5, 4]
The ^ operator performs element-wise bitwise XOR: , , and .
Question 40 of 60 · Binary operators · Medium
For a = np.array([1, 2, 3, 4]), what does (a % 2 == 0) | (a > 3) return?
Not quite. Try another option.
Answer: [False, True, False, True]
The first condition selects even values, and the second selects values greater than 3. Their element-wise OR selects 2 and 4.
Question 41 of 60 · Array manipulation · Hard
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))
Not quite. Try another option.
Answer: 4 False
The transpose is a view, but reshaping it in C order requires a copy. Therefore, modifying c does not change a, and the arrays do not share memory.
Question 42 of 60 · Array manipulation · Hard
What is the value of b[-1] after this code?
a = np.array([1, 2, 3, 4])
b = np.resize(a, (3, 3))
Not quite. Try another option.
Answer: array([3, 4, 1])
np.resize repeats the flattened input when the new array is larger. The nine values are 1, 2, 3, 4, 1, 2, 3, 4, 1.
Question 43 of 60 · Array manipulation · Hard
If x.shape is (2, 3, 4, 5), what is the shape of np.moveaxis(x, (0, 3), (2, 0))?
Not quite. Try another option.
Answer: (5, 3, 2, 4)
Original axis 3 moves to position 0, and original axis 0 moves to position 2. The resulting axis order is (3, 1, 0, 2).
Question 44 of 60 · Array manipulation · Hard
Which sequence of lists is produced by [part.tolist() for part in np.array_split(np.arange(10), 3)]?
Not quite. Try another option.
Answer: [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
np.array_split places the extra elements in the earliest chunks, producing lengths 4, 3, and 3.
Question 45 of 60 · Array manipulation · Hard
What is the final value of a?
a = np.zeros(4, dtype=int)
idx = np.array([0, 0, 2])
a[idx] += 1
Not quite. Try another option.
Answer: array([1, 0, 1, 0])
Advanced indexing uses a temporary array. The repeated index 0 is incremented in the temporary twice from the same original value, then written back once.
Question 46 of 60 · Array manipulation · Hard
What happens when np.empty((0, 3)).reshape(-1, 0) is evaluated?
Not quite. Try another option.
Answer: A ValueError is raised
With zero total elements and another dimension already equal to zero, NumPy cannot uniquely infer the dimension represented by -1.
Question 47 of 60 · Array manipulation · Hard
Given two arrays a and b, each with shape (2, 3), what is the shape of np.stack((a, b), axis=-1)?
Not quite. Try another option.
Answer: (2, 3, 2)
stack creates a new axis. With axis=-1, the new length-2 axis is appended after the existing two axes.
Question 48 of 60 · Broadcasting · Hard
What is the result shape of adding arrays with shapes (2, 1, 3, 1) and (1, 4, 1, 5)?
Not quite. Try another option.
Answer: (2, 4, 3, 5)
Each aligned pair of dimensions is equal or contains a 1. Taking the non-unit size at each position gives (2, 4, 3, 5).
Question 49 of 60 · Broadcasting · Hard
Which pair of shapes cannot be broadcast together under NumPy's standard broadcasting rules?
Not quite. Try another option.
Answer: (2, 3, 1) and (3, 4, 1)
The aligned dimensions include incompatible pairs 2 versus 3 and 3 versus 4. Neither value in either pair is 1.
Question 50 of 60 · Broadcasting · Hard
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
Not quite. Try another option.
Answer: array([0, 1, 2])
v broadcasts from (3, 1) across the last dimension and then across the leading dimension of a.
Question 51 of 60 · Broadcasting · Hard
What happens in the following code?
a = np.array([1, 2, 3])
b = np.broadcast_to(a, (4, 3))
b[0, 0] = 9
Not quite. Try another option.
Answer: A ValueError occurs because b is read-only
np.broadcast_to returns a read-only broadcasted view. Direct assignment through that view is therefore prohibited.
Question 52 of 60 · Broadcasting · Hard
What is the shape of the result when arrays with shapes (0, 3, 1) and (1, 1, 4) are added?
Not quite. Try another option.
Answer: (0, 3, 4)
A dimension of size 0 is compatible with a corresponding dimension of size 1. The result has shape (0, 3, 4) and contains no elements.
Question 53 of 60 · Broadcasting · Hard
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)
Not quite. Try another option.
Answer: array([[1, 20, 1], [10, 2, 30]])
x broadcasts across columns and y broadcasts across rows. The condition selects elementwise from those two broadcasted arrays.
Question 54 of 60 · Broadcasting · Hard
For matrix multiplication, A has shape (2, 1, 3, 4) and B has shape (5, 4, 6). What is the shape of A @ B?
Not quite. Try another option.
Answer: (2, 5, 3, 6)
The matrix dimensions (3, 4) and (4, 6) produce (3, 6). The batch shapes (2, 1) and (5,) broadcast to (2, 5).
Question 55 of 60 · Binary operators · Hard
What are the values of a // b and a % b?
a = np.array([-7, 7])
b = np.array([3, -3])
Not quite. Try another option.
Answer: array([-3, -3]) and array([2, -2])
NumPy floor division rounds toward negative infinity. The remainder follows and has the divisor's sign.
Question 56 of 60 · Binary operators · Hard
Assuming standard NumPy fixed-width integer arithmetic, what is produced by this expression?
Both operands have unsigned 8-bit dtype, so the result remains uint8. The value below zero wraps modulo , making .
Question 57 of 60 · Binary operators · Hard
What happens when the following expression is evaluated?
a = np.array([0, 2, 4])
a > 1 & a < 4
Not quite. Try another option.
Answer: A ValueError about ambiguous truth values is raised
Bitwise & binds more tightly than comparisons, and the comparisons form a chain. Python then attempts to convert an intermediate Boolean array to one truth value, which is ambiguous.
Question 58 of 60 · Binary operators · Hard
What values are printed by this code?
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
print(A @ b, b @ A)
Not quite. Try another option.
Answer: [17 39] [23 34]
A @ b computes row-vector dot products, while b @ A treats b as a row vector and computes dot products with the columns of A.
Question 59 of 60 · Binary operators · Hard
What happens when np.array([2, 4], dtype=int) ** -1 is evaluated?
Not quite. Try another option.
Answer: A ValueError is raised
NumPy does not allow a negative integer exponent when the base array has an integer dtype, because the mathematical results generally require reciprocals.
Question 60 of 60 · Binary operators · Hard
What is the result of a ^ b & a for the arrays below?
a = np.array([True, False, True])
b = np.array([True, True, False])
Not quite. Try another option.
Answer: array([False, False, True])
Bitwise & has higher precedence than ^, so the expression is a ^ (b & a). Applying the operations elementwise gives [False, False, True].
That's unit 7
Answer a few questions and your score shows up here.
Nearly through. Did unit 7 of ECAP776 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 7 of ECAP776 save you a night before the exam?
Then you can help keep it free for the next batch. 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.