Unit 5: Matrices - Practice Quiz

CSR101 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Python module is commonly used to generate random numbers?

Random number generation Easy
A. numbers
B. mathrandom
C. random
D. generate

2 What does np.random.randint(1, 5) generate?

Random number generation Easy
A. A random integer from 1 to 4
B. A fixed integer equal to 5
C. A random float from 1 to 5
D. A random integer from 1 to 5

3 What is the main purpose of NumPy in Python?

NumPy Easy
A. Creating web pages
B. Sending email messages
C. Managing text files
D. Working with numerical arrays

4 Which statement correctly imports NumPy using the common alias?

NumPy Easy
A. include numpy as np
B. import numpy as np
C. import np as numpy
D. using numpy as np

5 If a = np.array([10, 20, 30]), what is the value of a[1]?

Indexing and slicing Easy
A. 30
B. 1
C. 10
D. 20

6 For a two-dimensional array A, what does A[0, 1] select?

Indexing and slicing Easy
A. Second row, first column
B. Second row, second column
C. First row, first column
D. First row, second column

7 What elements are selected by a[1:4] for a one-dimensional NumPy array?

Indexing and slicing Easy
A. Indices 0 through 3
B. Only index 4
C. Indices 1 through 4
D. Indices 1 through 3

8 Which NumPy array attribute gives the number of dimensions?

Attributes of a NumPy array Easy
A. size
B. shape
C. ndim
D. dtype

9 What information does the shape attribute provide?

Attributes of a NumPy array Easy
A. Array dimensions
B. Largest array value
C. Element data type
D. Total memory usage

10 Which attribute returns the total number of elements in a NumPy array?

Attributes of a NumPy array Easy
A. ndim
B. size
C. shape
D. item

11 If a = np.array([1, 2]) and b = np.array([3, 4]), what is a + b?

Basic mathematical operations Easy
A. [3, 8]
B. [1, 2, 3, 4]
C. [4, 6]
D. [2, 2]

12 Which operator performs element-wise multiplication for NumPy arrays?

Basic mathematical operations Easy
A. //
B. *
C. @
D. **

13 Which NumPy function calculates the sum of array elements?

Basic mathematical operations Easy
A. np.addall()
B. np.sum()
C. np.plus()
D. np.total()

14 Which function changes the shape of an array without changing its data?

Array manipulation functions Easy
A. sort()
B. reshape()
C. reverse()
D. flatten_rows()

15 What does np.concatenate() generally do?

Array manipulation functions Easy
A. Joins arrays
B. Sorts numbers
C. Deletes arrays
D. Generates random values

16 What is the main purpose of matrix decomposition?

Matrix decomposition Easy
A. Removing every matrix row
B. Converting numbers into strings
C. Representing a matrix as simpler matrices
D. Changing all values to zero

17 In an LU decomposition, what do the letters L and U usually represent?

Matrix decomposition Easy
A. Linear and unit vectors
B. Lower and upper triangular matrices
C. Large and uniform matrices
D. Left and upper rows

18 What is a sparse matrix?

Sparse matrices and associated data structures Easy
A. A matrix with many zero elements
B. A matrix containing only text
C. A matrix with no zero elements
D. A matrix with one row

19 Which SciPy submodule provides sparse matrix structures?

Sparse matrices and associated data structures Easy
A. scipy.random
B. scipy.text
C. scipy.files
D. scipy.sparse

20 What is SciPy mainly used for?

SciPy Easy
A. Scientific and technical computing
B. Designing slide presentations
C. Building HTML layouts
D. Managing operating system users

21 What is the main purpose of setting a seed before generating random numbers with NumPy?

Random number generation Medium
A. To make generated values reproducible
B. To increase the number of generated values
C. To force all values to be identical
D. To convert random values into integers

22 What is the shape of the array returned by np.random.default_rng(3).uniform(size=(2, 4))?

Random number generation Medium
A. (4, 2)
B. (2, 4)
C. (2, 4, 1)
D. (8,)

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

NumPy Medium
A. [4, 5, 6, 1, 2, 3]
B. [1, 2, 3, 4, 5, 6]
C. [4, 10, 18]
D. [5, 7, 9]

24 Which expression creates a NumPy array containing zeros with floating-point data type?

NumPy Medium
A. np.zeros((3, 3), dtype=float)
B. np.zeros(3, 3, dtype=float)
C. np.ones((3, 3), dtype=float)
D. np.empty((3, 3), dtype=int)

25 Given A = np.array([[2, 4, 6], [8, 10, 12], [14, 16, 18]]), what does A[1:, :2] return?

Indexing and slicing Medium
A. [[4, 6], [10, 12]]
B. [[8, 10], [14, 16]]
C. [[2, 4], [8, 10]]
D. [[8, 10, 12], [14, 16, 18]]

26 For A = np.arange(12).reshape(3, 4), which expression selects the last column as a one-dimensional array?

Indexing and slicing Medium
A. A[-1, -1]
B. A[-1, :]
C. A[:, :-1]
D. A[:, -1]

27 If A = np.zeros((2, 3, 4), dtype=np.int32), what are A.ndim and A.size?

Attributes of a NumPy array Medium
A. ndim is 2 and size is 24
B. ndim is 3 and size is 9
C. ndim is 3 and size is 24
D. ndim is 4 and size is 24

28 An array has shape == (5, 2) and dtype == np.float64. Which statement about A.itemsize is correct?

Attributes of a NumPy array Medium
A. It is 5 bytes
B. It is 2 bytes
C. It is 10 bytes
D. It is 8 bytes

29 Given A = np.array([[1, 2], [3, 4]]), what is the result of A @ A?

Basic mathematical operations Medium
A. [[2, 4], [6, 8]]
B. [[7, 10], [15, 22]]
C. [[1, 4], [9, 16]]
D. [[5, 5], [5, 5]]

30 For A = np.array([[1, 2], [3, 4]]), what is the result of A * 2 + 1?

Basic mathematical operations Medium
A. [[2, 4], [6, 8]]
B. [[3, 5], [7, 9]]
C. [[3, 4], [5, 6]]
D. [[4, 6], [8, 10]]

31 What does np.sum(A, axis=0) calculate for a two-dimensional array A?

Basic mathematical operations Medium
A. The sum of the main diagonal
B. The sum of each column
C. The sum of each row
D. The sum of all elements as a matrix

32 What is the shape of np.arange(12).reshape(4, 3).T?

Array manipulation functions Medium
A. (4, 3)
B. (1, 12)
C. (12, 1)
D. (3, 4)

33 If a = np.array([1, 2]) and b = np.array([3, 4]), which expression produces [[1, 2], [3, 4]]?

Array manipulation functions Medium
A. np.column_stack((a, b))
B. np.vstack((a, b))
C. np.hstack((a, b))
D. np.concatenate((a, b), axis=1)

34 What is the effect of np.where(A > 5, A, 0) on an array A?

Array manipulation functions Medium
A. It replaces values greater than 5 with 0
B. It replaces values less than or equal to 5 with 0
C. It returns the indices of values greater than 5
D. It removes values less than or equal to 5

35 For a square matrix , which equation describes an LU decomposition when is included for pivoting?

Matrix decomposition Medium
A.
B.
C.
D.

36 Which decomposition is most directly associated with expressing a matrix as , where has orthonormal columns?

Matrix decomposition Medium
A. QR decomposition
B. Singular value decomposition
C. LU decomposition
D. Cholesky decomposition

37 Which sparse matrix format is generally efficient for matrix-vector multiplication and row slicing?

Sparse matrices and associated data structures Medium
A. COO
B. CSC
C. DOK
D. CSR

38 A sparse matrix has shape and only 200 nonzero values. What is a key advantage of storing it in a sparse format?

Sparse matrices and associated data structures Medium
A. It guarantees faster performance for every operation
B. It converts the matrix into a one-dimensional array
C. It stores mainly nonzero values and their positions
D. It changes all missing values into random values

39 Which SciPy function is appropriate for solving a square linear system ?

SciPy Medium
A. scipy.linalg.det(A, b)
B. scipy.linalg.inv(b, A)
C. scipy.linalg.solve(A, b)
D. scipy.linalg.eig(A, b)

40 Which vectorized expression returns the squares of all elements in a NumPy array x without an explicit Python loop?

Vectorization of code Medium
A. x @ x
B. x ** 2
C. x // 2
D. x ^ 2

41 Given rng = np.random.default_rng(123), which statement best explains why calling rng.integers(0, 10, 5) twice generally produces different arrays, while creating a new np.random.default_rng(123) before each call produces identical arrays?

Random number generation Hard
A. The generator updates its internal state after each draw.
B. The generator changes its probability distribution after each draw.
C. The upper bound becomes inclusive after the first draw.
D. The array shape changes the generator's seed automatically.

42 What is the shape of the result of rng.normal(size=(2, 1, 3)) + rng.normal(size=(1, 4, 1)), assuming both calls use the same Generator but independent draws?

Random number generation Hard
A. (2, 4, 3)
B. (2, 1, 3)
C. The operation raises a broadcasting error.
D. (1, 4, 3)

43 Let a = np.array([[1, 2], [3, 4]]) and b = np.array([[5, 6], [7, 8]]). Which expression produces the matrix product rather than elementwise multiplication?

NumPy Hard
A. np.multiply(a, b)
B. np.broadcast_to(a, b.shape)
C. a * b
D. a @ b

44 What is the dtype of np.array([1, 2], dtype=np.int8) + np.array([300], dtype=np.int16) on standard NumPy type-promotion rules?

NumPy Hard
A. float64
B. int16
C. int8
D. int32

45 Consider a = np.arange(12).reshape(3, 4). Which statement is correct about x = a[:, 1:3] and y = a[:, [1, 2]]?

Indexing and slicing Hard
A. Both are always views sharing memory with a.
B. Both are always independent copies of a.
C. x is an advanced-indexing copy, whereas y is usually a view.
D. x is usually a view, whereas y is an advanced-indexing copy.

46 For a = np.arange(20).reshape(4, 5), what is the shape of a[[0, 2], 1:4]?

Indexing and slicing Hard
A. (2, 4)
B. (4, 3)
C. (3, 2)
D. (2, 3)

47 Given a = np.array([10, 20, 30, 40]) and idx = np.array([1, 1, 3]), what is the result of a[idx] += 5?

Indexing and slicing Hard
A. [10, 25, 30, 50]
B. [10, 30, 30, 45]
C. [10, 20, 30, 45]
D. [10, 25, 30, 45]

48 For a = np.arange(24, dtype=np.float64).reshape(2, 3, 4), which tuple gives (a.ndim, a.shape, a.size, a.itemsize)?

Attributes of a NumPy array Hard
A. (3, (2, 3, 4), 24, 64)
B. (3, (2, 3, 4), 24, 8)
C. (2, (3, 4), 24, 8)
D. (3, (2, 3, 4), 48, 8)

49 For a C-contiguous array a = np.arange(12).reshape(3, 4), what is the expected value of a.strides on a platform where an integer occupies 8 bytes?

Attributes of a NumPy array Hard
A. (32, 8)
B. (24, 8)
C. (32, 4)
D. (8, 32)

50 For x = np.array([1.0, 2.0, 4.0]), which expression computes the Euclidean norm without explicitly forming x @ x?

Basic mathematical operations Hard
A. np.sqrt(np.sum(x ** 2))
B. np.sqrt(np.sum(x) ** 2)
C. np.sum(x ** 0.5) ** 2
D. np.sum(np.sqrt(x ** 2))

51 Let A have shape (3, 4) and b have shape (4,). Which expression subtracts the column vector represented by b from every row of A using broadcasting?

Basic mathematical operations Hard
A. A - b
B. A - b[None, :]
C. A - A.T @ b
D. A - b[:, None]

52 For a numerically stable computation of log(exp(x).sum()) when x may contain very large values, which expression is preferred?

Basic mathematical operations Hard
A. scipy.special.logsumexp(x)
B. np.log(np.exp(x).sum())
C. np.exp(np.log(x).sum())
D. np.log10(np.exp(x).sum())

53 Let a = np.arange(6). What is the key difference between a.reshape(2, 3) and np.resize(a, (2, 4))?

Array manipulation functions Hard
A. reshape changes dtype; resize preserves the original dtype.
B. reshape sorts values; resize preserves only unique values.
C. reshape requires the same element count; resize may repeat or truncate data.
D. reshape always copies; resize always returns a view.

54 For a = np.array([[1, 2], [3, 4]]), what is the shape of np.concatenate([a, a], axis=0) compared with np.concatenate([a, a], axis=1)?

Array manipulation functions Hard
A. (2, 4) and (4, 2)
B. (2, 2) and (2, 2)
C. (4, 4) and (4, 4)
D. (4, 2) and (2, 4)

55 Which operation converts a two-dimensional array A into a one-dimensional view when the memory layout permits it, without changing the element order?

Array manipulation functions Hard
A. A.transpose()
B. A.flatten()
C. A.squeeze(axis=0)
D. A.ravel()

56 If an matrix has singular values , which truncated SVD gives the best rank- approximation in the Frobenius norm?

Matrix decomposition Hard
A. U[:, :k] @ np.diag(s[:k]) @ Vt[:k, :]
B. U[:, k:] @ np.diag(s[k:]) @ Vt[k:, :]
C. U[:k, :] @ np.diag(s[:k]) @ Vt[:, :k]
D. U @ np.diag(s) @ Vt[:k, :]

57 For a nonsingular matrix , which SciPy operation is generally preferable to explicitly computing np.linalg.inv(A) when solving ?

Matrix decomposition Hard
A. np.linalg.eig(A) @ b
B. scipy.linalg.det(A) * b
C. scipy.linalg.solve(A, b)
D. scipy.linalg.norm(A) @ b

58 A COO sparse matrix is constructed with row indices [0, 0], column indices [1, 1], and data [2, 3]. After conversion to CSR format, what value is stored at position (0, 1) under standard duplicate-handling behavior?

Sparse matrices and associated data structures Hard
A. 2
B. 3
C. 5
D. The conversion raises an error.

59 Which sparse format is usually most suitable for efficient row slicing and matrix-vector multiplication after construction?

Sparse matrices and associated data structures Hard
A. CSC
B. DOK
C. CSR
D. COO

60 For a symmetric positive-definite matrix , which factorization is most appropriate and commonly more efficient than a general LU factorization?

SciPy Hard
A. Schur decomposition
B. Cholesky factorization
C. Singular value decomposition
D. Generalized eigenvalue decomposition