Unit 4: Array Operations using NumPy - Practice Quiz

ECE181 — Introduction To Python 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Python library provides the ndarray object used for numerical arrays?

arrays vs lists Easy
A. Matplotlib
B. Pandas
C. NumPy
D. SciPy

2 What is a key difference between a NumPy array and a Python list?

arrays vs lists Easy
A. A NumPy array cannot be indexed
B. A list can only hold numbers
C. A list uses less memory than an array
D. A NumPy array stores elements of a single data type

3 Which function is used to create a NumPy array from a Python list?

arrays vs lists Easy
A. np.create()
B. np.array()
C. np.list()
D. np.make()

4 Compared to Python lists, NumPy arrays are generally preferred for numerical computation because they are:

arrays vs lists Easy
A. Slower but easier to read
B. Faster and more memory efficient
C. Limited to one element only
D. Unable to store integers

5 Which attribute is used to check the data type of elements in a NumPy array?

data types Easy
A. .dtype
B. .type
C. .datatype
D. .kind

6 What does the NumPy data type int64 represent?

data types Easy
A. A 64-bit integer
B. A 64-bit floating point number
C. A 64-character string
D. A 64-element array

7 Which method changes the data type of an existing NumPy array?

data types Easy
A. .convert()
B. .setdtype()
C. .astype()
D. .retype()

8 If an array is created as np.array([1.0, 2.0, 3.0]), its default data type will be:

data types Easy
A. bool
B. str
C. int32
D. float64

9 Given a = np.array([1, 2, 3]), what is the result of a + 2?

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

10 Which attribute returns the shape (dimensions) of a NumPy array?

array operations Easy
A. .dim
B. .size
C. .shape
D. .length

11 Given a = np.array([1, 2, 3]) and b = np.array([4, 5, 6]), what is a * b?

array operations Easy
A. [4, 5, 6]
B. [1, 2, 3]
C. [5, 7, 9]
D. [4, 10, 18]

12 Which function reshapes an array without changing its data?

array operations Easy
A. resize()
B. reshape()
C. restack()
D. reform()

13 What does np.arange(0, 5) produce?

array operations Easy
A. [0, 1, 2, 3, 4, 5]
B. [0, 5]
C. [1, 2, 3, 4, 5]
D. [0, 1, 2, 3, 4]

14 Which NumPy function computes the average of array elements?

statistical functions Easy
A. np.center()
B. np.mean()
C. np.middle()
D. np.avg()

15 What does np.max() return for a NumPy array?

statistical functions Easy
A. The sum of elements
B. The number of elements
C. The largest element
D. The smallest element

16 Which function computes the standard deviation of a NumPy array?

statistical functions Easy
A. np.var()
B. np.dev()
C. np.std()
D. np.spread()

17 Given a = np.array([2, 4, 6]), what does np.sum(a) return?

statistical functions Easy
A. 12
B. 6
C. 4
D. 3

18 What does broadcasting in NumPy allow you to do?

broadcasting Easy
A. Send data over a network
B. Print arrays to screen
C. Operate on arrays of different shapes
D. Sort arrays automatically

19 When you add a scalar to a NumPy array, broadcasting causes the scalar to be:

broadcasting Easy
A. Ignored entirely
B. Added only to the first element
C. Converted to a string
D. Applied to every element

20 For broadcasting to work between two arrays, their dimensions must be:

broadcasting Easy
A. Prime numbers
B. Both greater than 10
C. Compatible or equal to 1
D. Exactly identical always

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)

arrays vs lists Medium
A. [2, 4, 6] [1 2 3 1 2 3]
B. [1, 2, 3, 1, 2, 3] [2 4 6]
C. [1, 2, 3, 1, 2, 3] [1 2 3 1 2 3]
D. [2, 4, 6] [2 4 6]

22 Which statement best explains why NumPy arrays are generally faster than Python lists for numerical computation?

arrays vs lists Medium
A. Arrays automatically use multiple CPU cores for every operation
B. Arrays store each element as a separate Python object with pointers
C. Arrays store homogeneous data in contiguous memory and use vectorized C operations
D. Arrays are interpreted line by line while lists are compiled

23 What will be the dtype of the array created below?
python
arr = np.array([1, 2, 3.0, 4])

data types Medium
A. float64
B. complex128
C. object
D. 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])

data types Medium
A. 127
B. 300
C. Raises an OverflowError
D. 44

25 Which code correctly converts a float array a into an integer array without changing the original?

data types Medium
A. b = a.astype(np.int32)
B. b = a.convert('int32')
C. b = a.dtype(np.int32)
D. b = int(a)

26 Given arr = np.arange(12).reshape(3, 4), what does arr[:, 1] return?

array operations Medium
A. [1 5 9]
B. [0 1 2 3]
C. [4 5 6 7]
D. [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))

array operations Medium
A. [3 7]
B. [4 6]
C. [6 4]
D. 10

28 Given arr = np.array([10, 20, 30, 40, 50]), what does arr[::-2] return?

array operations Medium
A. [50 40 30 20 10]
B. [50 30 10]
C. [10 30 50]
D. [40 20]

29 What is the difference between np.dot(a, b) and a * b for two 2D arrays of the same shape?

array operations Medium
A. Both perform matrix multiplication but * is faster
B. np.dot performs element-wise multiplication while * performs matrix multiplication
C. np.dot performs matrix multiplication while * performs element-wise multiplication
D. Both perform element-wise multiplication with different rounding

30 What is the output of the following code?
python
arr = np.array([1, 2, 3, 4, 5])
print(arr[arr % 2 == 0])

array operations Medium
A. [2 4]
B. [True False True False True]
C. [0 2 0 4 0]
D. [1 3 5]

31 Given arr = np.array([2, 4, 6, 8]), what does np.std(arr) compute and its approximate value?

statistical functions Medium
A. The variance,
B. Sample standard deviation,
C. The range,
D. Population standard deviation,

32 For a = np.array([[1, 2], [3, 4]]), what does np.mean(a, axis=1) return?

statistical functions Medium
A. 2.5
B. [4. 6.]
C. [2. 3.]
D. [1.5 3.5]

33 Which function returns the index of the maximum value in an array rather than the value itself?

statistical functions Medium
A. np.amax
B. np.maximum
C. np.argmax
D. 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))

statistical functions Medium
A. 4.0
B. 3.5
C. 1.0
D. 3.0

35 Given arr = np.array([10, 20, 30, 40]), what does np.cumsum(arr) return?

statistical functions Medium
A. 100
B. [100 90 70 40]
C. [10 30 60 100]
D. [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)

broadcasting Medium
A. Raises a ValueError
B. (3, 3)
C. (3,)
D. (3, 1)

37 Which pair of array shapes CANNOT be broadcast together?

broadcasting Medium
A. (4, 3) and (2,)
B. (2, 3) and (1, 3)
C. (5, 1) and (1, 6)
D. (3, 4) and (4,)

38 What is the output of the following code?
python
a = np.array([1, 2, 3])
print(a + 5)

broadcasting Medium
A. [5 5 5]
B. Raises a ValueError
C. [1 2 3 5]
D. [6 7 8]

39 Given a of shape (2, 3) and b of shape (3,), what is the shape of a * b?

broadcasting Medium
A. (2, 3)
B. Raises a ValueError
C. (3,)
D. (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])

array operations Medium
A. 0
B. 99
C. 1
D. Raises an error

41 Consider the following code:

PYTHON
import numpy as np
lst = [1, 2, 3]
arr = np.array([1, 2, 3])
print(lst * 2)
print(arr * 2)



What is the output?

arrays vs lists Hard
A. [2, 4, 6] and [1 2 3 1 2 3]
B. [1, 2, 3, 1, 2, 3] and [2 4 6]
C. [1, 2, 3, 1, 2, 3] and [1 2 3 1 2 3]
D. [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?

arrays vs lists Hard
A. It offloads storage to disk and keeps only pointers in RAM
B. It stores only unique values and references duplicates
C. It stores raw values in a contiguous, fixed-type buffer without per-element object overhead
D. It compresses the integers using run-length encoding automatically

43 What does the following code print?

PYTHON
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int8)
arr[0] = 130
print(arr[0])

data types Hard
A. -126
B. 127
C. 2
D. 130

44 Given arr = np.array([1, 2, 3]), which operation returns a new array of type float64 without modifying arr?

data types Hard
A. arr.dtype = np.float64
B. arr.reshape(np.float64)
C. arr.view(np.float64)
D. arr.astype(np.float64)

45 What is the resulting dtype of np.array([True, 2, 3.5])?

data types Hard
A. bool
B. int64
C. object
D. float64

46 What does the following print?

PYTHON
import numpy as np
a = np.arange(6).reshape(2, 3)
b = a.T
b[0, 0] = 99
print(a[0, 0])

array operations Hard
A. 0
B. 5
C. 99
D. Raises an error

47 Given a = np.array([10, 20, 30, 40]), what does a[::-1][1:3] return?

array operations Hard
A. array([30, 20])
B. array([20, 30])
C. array([30, 40])
D. array([40, 30])

48 Consider:

PYTHON
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?

array operations Hard
A. [1 2 3 4 5]
B. [0 0 0 0 0]
C. [1 0 3 0 5]
D. [0 2 0 4 0]

49 What is the result of np.arange(12).reshape(3, 4)[:, 1::2]?

array operations Hard
A. [[1 2], [5 6], [9 10]]
B. [[2 3], [6 7], [10 11]]
C. [[0 2], [4 6], [8 10]]
D. [[1 3], [5 7], [9 11]]

50 Which statement about np.reshape(a, (-1, 3)) is correct when a has 12 elements?

array operations Hard
A. The -1 is treated as , producing an empty array
B. The -1 reverses the array before reshaping
C. It raises an error because -1 is not a valid dimension
D. The -1 is inferred as , producing a array

51 For a = np.array([[1, 2], [3, 4]]), what does a.mean(axis=0) return?

statistical functions Hard
A. array([2., 3.])
B. array([1.5, 3.5])
C. 2.5
D. 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)?

statistical functions Hard
A. 4.0
B. 5.0
C. 2.14
D. 2.0

53 Given an array containing a np.nan, which function ignores the NaN when computing the mean?

statistical functions Hard
A. np.mean
B. np.nanmean
C. np.average
D. np.mean with axis=None

54 What is the difference between a.var() and a.var(ddof=1) for a sample array?

statistical functions Hard
A. ddof=1 computes standard deviation instead of variance
B. They are identical for all arrays
C. ddof=1 divides by (sample variance) instead of (population variance)
D. ddof=1 divides by instead of

55 For a = np.array([[1, 5], [3, 2]]), what does np.argmax(a) return?

statistical functions Hard
A. 5
B. 1
C. (0, 1)
D. 3

56 Given a of shape (3, 1) and b of shape (1, 4), what is the shape of a + b?

broadcasting Hard
A. (1, 4)
B. (3, 1)
C. (3, 4)
D. Raises a broadcasting error

57 Which pair of shapes CANNOT be broadcast together?

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

58 What does the following produce?

PYTHON
import numpy as np
a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30])
print((a + b).shape)

broadcasting Hard
A. (3, 1)
B. (9,)
C. (1, 3)
D. (3, 3)

59 To subtract the per-column mean from each element of a (100, 5) array X, which expression works via broadcasting?

broadcasting Hard
A. X - X.mean(axis=1)
B. X - X.mean(axis=1).reshape(-1, 1) giving column-mean subtraction
C. X - X.mean(axis=0)
D. X - X.mean()

60 What is the result of the following?

PYTHON
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[0], [1], [2]])
print(a * b)

broadcasting Hard
A. [[1 2 3], [1 2 3], [1 2 3]]
B. [[0 2 6]]
C. [[0 0 0], [1 2 3], [2 4 6]]
D. Raises a broadcasting error