Unit 7: Operations on NumPy arrays - Practice Quiz

ECAP776 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which NumPy method changes the shape of an array without changing its data?

Array manipulation Easy
A. reshape()
B. resize_data()
C. reorder()
D. convert()

2 Which attribute returns the dimensions of a NumPy array?

Array manipulation Easy
A. dtype
B. shape
C. size
D. itemsize

3 What does the flatten() method return?

Array manipulation Easy
A. A transposed array view
B. A one-dimensional copy
C. A two-dimensional view
D. A sorted array copy

4 Which NumPy function joins arrays along an existing axis?

Array manipulation Easy
A. np.reshape()
B. np.split()
C. np.transpose()
D. np.concatenate()

5 What does arr.T represent for a two-dimensional NumPy array?

Array manipulation Easy
A. The transposed array
B. The flattened array
C. The reversed array
D. The duplicated array

6 Which NumPy function divides an array into multiple subarrays?

Array manipulation Easy
A. np.append()
B. np.squeeze()
C. np.stack()
D. np.split()

7 What is the shape of np.array([1, 2, 3, 4]).reshape(2, 2)?

Array manipulation Easy
A. (1, 4)
B. (2, 4)
C. (4, 1)
D. (2, 2)

8 What is broadcasting in NumPy?

Broadcasting Easy
A. Operating on compatible shapes
B. Converting arrays to lists
C. Sorting arrays by shape
D. Copying arrays to files

9 What happens when the scalar 5 is added to a NumPy array?

Broadcasting Easy
A. It is added to the array size
B. It is added to every element
C. It is added to the first element
D. It is added to the last element

10 Which pair of shapes is compatible for broadcasting?

Broadcasting Easy
A. (3, 4) and (3,)
B. (3, 4) and (5,)
C. (3, 4) and (2,)
D. (3, 4) and (4,)

11 When comparing dimensions during broadcasting, which value is compatible with any dimension size?

Broadcasting Easy
A. 1
B. -1
C. 2
D. 0

12 What is the result of np.array([1, 2, 3]) + 2?

Broadcasting Easy
A. [3, 4, 5]
B. [1, 2, 5]
C. [2, 4, 6]
D. [3, 2, 3]

13 For broadcasting, NumPy compares array shapes starting from which side?

Broadcasting Easy
A. The rightmost side
B. The topmost side
C. The leftmost side
D. The middle position

14 What is a key benefit of NumPy broadcasting?

Broadcasting Easy
A. It automatically sorts values
B. It avoids many explicit loops
C. It converts values to strings
D. It permanently changes shapes

15 Which operator performs element-wise addition on NumPy arrays?

Binary operators Easy
A. -
B. +
C. /
D. *

16 What is the result of np.array([2, 4]) * np.array([3, 5])?

Binary operators Easy
A. [6, 9]
B. [5, 9]
C. [2, 20]
D. [6, 20]

17 Which operator performs element-wise exponentiation in NumPy?

Binary operators Easy
A. @
B. //
C. %
D. **

18 Which operator performs matrix multiplication between two suitable NumPy arrays?

Binary operators Easy
A. %
B. @
C. //
D. *

19 What does the % operator calculate element-wise?

Binary operators Easy
A. Remainders
B. Powers
C. Products
D. Quotients

20 What is the result of np.array([5, 8]) > np.array([3, 10])?

Binary operators Easy
A. [True, False]
B. [True, True]
C. [False, False]
D. [False, True]

21 What is printed by the following code?

a = np.arange(6)

b = a.reshape(2, 3)

b[0, 0] = 99

print(a[0])

Array manipulation Medium
A. 0
B. 6
C. 99
D. 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]?

Array manipulation Medium
A. (4, 2, 3) and 19
B. (3, 4, 2) and 19
C. (2, 4, 3) and 23
D. (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)?

Array manipulation Medium
A. [[1, 0, 1], [1, 0, 1]]
B. [[1, 1, 0], [1, 1, 0]]
C. [[1, 1], [1, 1], [0, 0]]
D. [[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?

Array manipulation Medium
A. [[1], [2], [3], [4]]
B. [[1, 2], [3, 4]]
C. [1, 2, 3, 4]
D. [[1, 3], [2, 4]]

25 What are the lengths of the arrays returned by np.array_split(np.arange(10), 3)?

Array manipulation Medium
A. [4, 4, 2]
B. [3, 3, 4]
C. [3, 3, 3]
D. [4, 3, 3]

26 Given a = np.array([[1, 2, 3], [4, 5, 6]]), what does np.flip(a, axis=1) return?

Array manipulation Medium
A. [[1, 4], [2, 5], [3, 6]]
B. [[6, 5, 4], [3, 2, 1]]
C. [[4, 5, 6], [1, 2, 3]]
D. [[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?

Array manipulation Medium
A. [[9, 2], [3, 4]]
B. [[1, 2], [3, 4]]
C. [[1, 9], [3, 4]]
D. [[9, 2], [9, 4]]

28 What is the shape of the result when arrays with shapes (3, 1) and (1, 4) are added?

Broadcasting Medium
A. (4, 3)
B. (1, 4)
C. (3, 1)
D. (3, 4)

29 What is the result of np.array([[1, 2, 3], [4, 5, 6]]) + np.array([10, 20, 30])?

Broadcasting Medium
A. [[31, 32, 33], [34, 35, 36]]
B. [[11, 22, 33], [14, 25, 36]]
C. [[11, 12, 13], [24, 25, 26]]
D. [[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?

Broadcasting Medium
A. (2, 3, 1)
B. (2, 3, 4)
C. (3, 3, 4)
D. (2, 1, 4)

31 What happens when an array of shape (4, 3) is added to an array of shape (4,)?

Broadcasting Medium
A. ValueError is raised
B. A result of shape (4, 4) is created
C. A result of shape (3, 4) is created
D. A result of shape (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?

Broadcasting Medium
A. a - a.mean(axis=1)
B. a - a.mean(axis=1, keepdims=True)
C. a - a.mean()
D. 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?

Broadcasting Medium
A. (2, 3)
B. (1, 2)
C. (3, 1)
D. (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])?

Broadcasting Medium
A. [[1, 2, 3], [0, 0, 0]]
B. [[1, 1, 1], [2, 2, 2]]
C. [[1, 2, 0], [3, 0, 0]]
D. [[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?

Binary operators Medium
A. [4, 10, 18]
B. [4, 5, 6]
C. [5, 7, 9]
D. 32

36 What is the result of np.array([[1, 2], [3, 4]]) @ np.array([5, 6])?

Binary operators Medium
A. [11, 25]
B. [23, 34]
C. [17, 39]
D. [5, 12]

37 For a = np.array([0, 1, 2, 3, 4]), what does (a > 1) & (a < 4) return?

Binary operators Medium
A. [False, False, True, True, True]
B. [True, True, False, False, True]
C. [False, False, True, True, False]
D. [False, True, True, True, False]

38 What is the result of np.array([-7, 7]) // 3?

Binary operators Medium
A. [-3, 2]
B. [-2, 3]
C. [-3, 3]
D. [-2, 2]

39 What does np.array([5, 6, 7]) ^ np.array([3, 3, 3]) produce?

Binary operators Medium
A. [7, 7, 7]
B. [2, 3, 4]
C. [1, 2, 3]
D. [6, 5, 4]

40 For a = np.array([1, 2, 3, 4]), what does (a % 2 == 0) | (a > 3) return?

Binary operators Medium
A. [False, True, True, True]
B. [True, False, True, False]
C. [False, True, False, True]
D. [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))

Array manipulation Hard
A. 4 False
B. -1 True
C. -1 False
D. 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 manipulation Hard
A. array([4, 0, 0])
B. array([1, 2, 3])
C. array([3, 4, 1])
D. 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))?

Array manipulation Hard
A. (3, 5, 2, 4)
B. (5, 4, 2, 3)
C. (5, 3, 2, 4)
D. (5, 2, 3, 4)

44 Which sequence of lists is produced by [part.tolist() for part in np.array_split(np.arange(10), 3)]?

Array manipulation Hard
A. [[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]
B. [[0, 1, 2], [3, 4, 5, 6], [7, 8, 9]]
C. [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
D. [[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 manipulation Hard
A. array([2, 0, 1, 0])
B. array([1, 1, 1, 0])
C. array([1, 0, 1, 0])
D. array([2, 0, 2, 0])

46 What happens when np.empty((0, 3)).reshape(-1, 0) is evaluated?

Array manipulation Hard
A. An array of shape (0, 0) is returned
B. An array of shape (0, 3) is returned
C. A ValueError is raised
D. An array of shape (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)?

Array manipulation Hard
A. (4, 3)
B. (2, 6)
C. (2, 3, 2)
D. (2, 2, 3)

48 What is the result shape of adding arrays with shapes (2, 1, 3, 1) and (1, 4, 1, 5)?

Broadcasting Hard
A. (1, 4, 3, 5)
B. (2, 4, 3, 5)
C. (2, 4, 1, 5)
D. (2, 1, 3, 5)

49 Which pair of shapes cannot be broadcast together under NumPy's standard broadcasting rules?

Broadcasting Hard
A. (2, 3, 1) and (3, 4, 1)
B. (2, 1, 4) and (3, 4)
C. (3, 2, 1) and (2, 4)
D. (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

Broadcasting Hard
A. array([1, 1, 1])
B. array([0, 1, 2])
C. array([2, 2, 2])
D. 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

Broadcasting Hard
A. a[0] and the first column become equal to 9
B. A ValueError occurs because b is read-only
C. Only b[0, 0] becomes equal to 9
D. The entire first row of 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?

Broadcasting Hard
A. The operation raises ValueError
B. (0, 1, 4)
C. (1, 3, 4)
D. (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)

Broadcasting Hard
A. array([[1, 20, 1], [10, 2, 30]])
B. array([[1, 10, 1], [20, 2, 20]])
C. array([[10, 1, 30], [2, 20, 2]])
D. 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?

Broadcasting Hard
A. (2, 1, 3, 6)
B. (2, 5, 3, 6)
C. (5, 2, 3, 6)
D. (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])

Binary operators Hard
A. array([-2, -2]) and array([-1, 1])
B. array([-3, -3]) and array([2, -2])
C. array([-2, -3]) and array([-1, -2])
D. 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)

Binary operators Hard
A. array([255, 1, 2], dtype=uint8)
B. array([0, 0, 1], dtype=uint8)
C. array([255, 0, 1], dtype=uint8)
D. 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

Binary operators Hard
A. The result is array([False, True, False])
B. A ValueError about ambiguous truth values is raised
C. The result is array([False, True, True])
D. A 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)

Binary operators Hard
A. [17 23] [34 39]
B. [15 24] [20 36]
C. [23 34] [17 39]
D. [17 39] [23 34]

59 What happens when np.array([2, 4], dtype=int) ** -1 is evaluated?

Binary operators Hard
A. array([0, 0]) is returned
B. array([0.5, 0.25]) is returned
C. array([1, 1]) is returned
D. A 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])

Binary operators Hard
A. array([False, True, True])
B. array([False, False, True])
C. array([True, True, False])
D. array([True, False, False])