Python Arrays MCQs

1 min read

Unlike many other programming languages, Python does not have a built-in static array data structure in its core syntax, instead relying heavily on flexible lists and the array module for homogeneous data types. For advanced numerical computations and multi-dimensional matrices, developers utilize the powerful NumPy library. Understanding how Python handles sequential memory allocation, list operations, and vectorization is crucial for optimizing code efficiency. These practice MCQs are designed to test your knowledge of Python’s sequential containers and core array operations.

1. Which of the following functions in NumPy creates an array filled entirely with zeros?

a) np.empty()
b) np.zeros()
c) np.ones()
d) np.full()
Correct Answer: b) np.zeros()
Explanation:
The np.zeros() function returns a new array of given shape and type, filled with zeros.

2. What is the output of len(np.array([[1, 2], [3, 4], [5, 6]])) in NumPy?

a) 6
b) 3
c) 2
d) 1
Correct Answer: b) 3
Explanation:
The len() function on a NumPy array returns the size of the first dimension (the number of rows), which is 3.

3. Which NumPy function is used to create an array with a range of numbers spaced evenly on a log scale?

a) np.arange()
b) np.linspace()
c) np.logspace()
d) np.geomspace()
Correct Answer: c) np.logspace()
Explanation:
np.logspace() returns numbers spaced evenly on a log scale, whereas np.linspace() uses a linear scale.

4. What does the ndarray.ndim attribute represent in NumPy?

a) The total number of elements
b) The number of dimensions (axes) of the array
c) The data type of the array elements
d) The size in bytes of each element
Correct Answer: b) The number of dimensions (axes) of the array
Explanation:
The ndim attribute returns an integer representing the number of dimensions or axes of the NumPy array.

5. Which type code represents a signed integer of size 2 bytes in Python's built-in 'array' module?

a) i
b) h
c) l
d) f
Correct Answer: b) h
Explanation:
In Python's built-in array module, 'h' represents a signed short integer (typically 2 bytes), while 'i' represents a signed integer.

6. How do you reshape a 1D NumPy array of 12 elements into a 3x4 matrix?

a) arr.resize(3, 4)
b) arr.reshape(3, 4)
c) np.shape(arr, (3, 4))
d) arr.shape(3, 4)
Correct Answer: b) arr.reshape(3, 4)
Explanation:
The reshape() method gives a new shape to an array without changing its data, provided the total number of elements matches.

7. What is the return type of arr.itemsize for a NumPy array with dtype=np.float64?

a) 4
b) 8
c) 2
d) 1
Correct Answer: b) 8
Explanation:
The itemsize attribute returns the length of each element in bytes. A float64 takes 8 bytes of memory.

8. Which function is used to flatten a multi-dimensional NumPy array into a 1D array?

a) arr.ravel()
b) arr.flatten()
c) Both arr.ravel() and arr.flatten()
d) arr.compress()
Correct Answer: c) Both arr.ravel() and arr.flatten()
Explanation:
Both methods return a contiguous flattened 1D array, though ravel() typically returns a view when possible while flatten() allocates a copy.

9. What happens when you perform element-wise addition on two NumPy arrays of different shapes that are broadcast-compatible?

a) A ValueError is raised
b) The smaller array is broadcast across the larger array
c) Only elements up to the smaller length are added
d) A TypeError is raised
Correct Answer: b) The smaller array is broadcast across the larger array
Explanation:
NumPy broadcasting rules allow arrays of different shapes to be combined mathematically by stretching the smaller array.

10. Which method of Python's built-in 'array' module writes array items to a file as a binary stream?

a) tofile()
b) write()
c) tofile_binary()
d) dump()
Correct Answer: a) tofile()
Explanation:
The tofile() method writes all items to a file object as machine values.

11. What is the output of np.array([True, False, True]) * 2?

a) [True, False, True]
b) [2, 0, 2]
c) [True, True, True]
d) TypeError
Correct Answer: b) [2, 0, 2]
Explanation:
In NumPy, boolean arrays can be treated as integers (True is 1, False is 0) during numeric operations.

12. Which function joins a sequence of arrays along an existing axis in NumPy?

a) np.concatenate()
b) np.union()
c) np.merge()
d) np.append()
Correct Answer: a) np.concatenate()
Explanation:
np.concatenate() joins a sequence of arrays along an existing axis.

13. What does np.eye(3) generate?

a) A 3x3 matrix of zeros
b) A 3x3 identity matrix with ones on the main diagonal
c) A 3x3 matrix of random values
d) A 1D array with values 0, 1, 2
Correct Answer: b) A 3x3 identity matrix with ones on the main diagonal
Explanation:
np.eye() creates a 2D array with ones on the diagonal and zeros elsewhere.

14. Which attribute of a NumPy array gives the total number of elements in the array?

a) length
b) count
c) size
d) nbytes
Correct Answer: c) size
Explanation:
The size attribute returns the total number of elements in the NumPy array.

15. What is the primary difference between np.linspace() and np.arange()?

a) linspace specifies the number of samples, while arange specifies the step size
b) arange only supports integer steps
c) linspace only works with floats
d) There is no difference
Correct Answer: a) linspace specifies the number of samples, while arange specifies the step size
Explanation:
np.linspace takes a count parameter for evenly spaced values, whereas np.arange takes a step size.

16. Which function can be used to find the indices of maximum values along an axis in a NumPy array?

a) np.max()
b) np.argmax()
c) np.findmax()
d) np.maximum()
Correct Answer: b) np.argmax()
Explanation:
np.argmax() returns the indices of the maximum values along a specified axis.

17. What does the astype() method do on a NumPy array?

a) Changes the shape of the array
b) Casts the array to a specified data type
c) Sorts the array elements
d) Checks the data type of the elements
Correct Answer: b) Casts the array to a specified data type
Explanation:
astype() is a copy function that casts the array to the specified dtype.

18. Which NumPy function stacks arrays vertically (row wise)?

a) np.hstack()
b) np.vstack()
c) np.dstack()
d) np.column_stack()
Correct Answer: b) np.vstack()
Explanation:
np.vstack() stacks arrays in sequence vertically (row wise).

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

a) [5, 7, 9]
b) [[1, 2, 3], [4, 5, 6]]
c) [1, 2, 3, 4, 5, 6]
d) 21
Correct Answer: a) [5, 7, 9]
Explanation:
Arithmetic operations on NumPy arrays are element-wise by default.

20. Which method in Python's built-in 'array' module removes the first occurrence of a specific value?

a) pop()
b) remove()
c) delete()
d) discard()
Correct Answer: b) remove()
Explanation:
The remove() method searches for the given value and removes the first matching element from the array.

21. What does np.empty() return when called?

a) An array filled with zeros
b) An array filled with ones
c) An uninitialized array with arbitrary garbage values in memory
d) An empty array of size 0
Correct Answer: c) An uninitialized array with arbitrary garbage values in memory
Explanation:
np.empty() allocates memory without initializing its values, making it faster than np.zeros().

22. Which module function reads an array from a file created with tofile() in Python's built-in array module?

a) fromfile()
b) readfile()
c) load()
d) open()
Correct Answer: a) fromfile()
Explanation:
The fromfile() function reads items from a file object and appends them to the array.

23. What is the result of boolean indexing like arr[arr > 5] on a NumPy array?

a) A boolean mask array
b) A 1D array containing only the elements greater than 5
c) The count of elements greater than 5
d) An error
Correct Answer: b) A 1D array containing only the elements greater than 5
Explanation:
Boolean indexing filters the array, returning a new 1D array with elements that satisfy the condition.

24. Which NumPy function calculates the statistical median of an array?

a) np.average()
b) np.mean()
c) np.median()
d) np.mid()
Correct Answer: c) np.median()
Explanation:
np.median() computes the median along the specified axis.

25. What does the transpose attribute .T do on a 2D NumPy array?

a) Flattens the array
b) Swaps the rows and columns
c) Reverses the element order
d) Inverts the matrix mathematically
Correct Answer: b) Swaps the rows and columns
Explanation:
The .T attribute is a shorthand for reversing or permuting the axes of an array, effectively transposing a 2D matrix.

26. Which function calculates the dot product of two 2D arrays in NumPy?

a) np.dot()
b) np.multiply()
c) np.cross()
d) np.product()
Correct Answer: a) np.dot()
Explanation:
np.dot() computes the matrix dot product for 2D arrays (or inner product for 1D arrays).

27. What is the time complexity of appending an element to the end of a standard Python list?

a) O(n)
b) O(1) amortized
c) O(log n)
d) O(n^2)
Correct Answer: b) O(1) amortized
Explanation:
Due to over-allocation strategies, appending to a Python list takes amortized constant time O(1).

28. Which function generates random floats in the half-open interval [0.0, 1.0) in NumPy?

a) np.random.randint()
b) np.random.random()
c) np.random.uniform()
d) np.random.choice()
Correct Answer: b) np.random.random()
Explanation:
np.random.random() returns random floats in the interval [0.0, 1.0).

29. What does the np.unique() function return?

a) The duplicate elements of an array
b) The sorted, unique elements of an array
c) The count of unique elements
d) A boolean mask of unique values
Correct Answer: b) The sorted, unique elements of an array
Explanation:
np.unique() finds the unique elements of an array and returns them in sorted order.

30. Which method removes and returns the last item in Python's built-in 'array' module?

a) delete()
b) pop()
c) remove()
d) extract()
Correct Answer: b) pop()
Explanation:
The pop() method removes the item at the given index (defaulting to the end of the array) and returns it.

31. What is the output of np.all([True, True, False])?

a) True
b) False
c) None
d) TypeError
Correct Answer: b) False
Explanation:
np.all() tests whether all array elements evaluate to True. Since one element is False, it returns False.

32. Which NumPy function is used to split an array into multiple sub-arrays?

a) np.split()
b) np.divide()
c) np.partition()
d) np.break()
Correct Answer: a) np.split()
Explanation:
np.split() splits an array into multiple sub-arrays as specified.

33. What does the ndarray.nbytes attribute return?

a) The number of dimensions
b) The total bytes consumed by the elements of the array
c) The size of a single element
d) The buffer address
Correct Answer: b) The total bytes consumed by the elements of the array
Explanation:
nbytes gives the total number of bytes consumed by the array data (itemsize * size).

34. Which function computes the standard deviation of a NumPy array?

a) np.var()
b) np.std()
c) np.deviation()
d) np.mean()
Correct Answer: b) np.std()
Explanation:
np.std() computes the standard deviation along the specified axis.

35. What is the result of slicing a 1D NumPy array like arr[1:5:2]?

a) A copy of elements at indices 1, 3, and 5
b) A view of elements from index 1 up to 5 with a step of 2
c) An error
d) A scalar value
Correct Answer: b) A view of elements from index 1 up to 5 with a step of 2
Explanation:
NumPy array slices create views (not copies) of the original array data.

36. Which function creates a sequence of numbers starting from 0 up to a stop value with a specified step in NumPy?

a) np.range()
b) np.arange()
c) np.sequence()
d) np.steps()
Correct Answer: b) np.arange()
Explanation:
np.arange() returns evenly spaced values within a given interval.

37. What does np.any([True, False, False]) evaluate to?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
np.any() tests whether any array element along a given axis evaluates to True.

38. Which method converts a Python built-in array object back into a standard Python list?

a) tolist()
b) list()
c) convert()
d) toArray()
Correct Answer: a) tolist()
Explanation:
The tolist() method converts the array items into a standard Python list.

39. What is the purpose of np.where(condition, x, y)?

a) Returns elements chosen from x or y depending on condition
b) Finds the index of condition
c) Filters the array in-place
d) Sorts the array based on condition
Correct Answer: a) Returns elements chosen from x or y depending on condition
Explanation:
np.where() yields elements from x where the condition is True, and from y elsewhere.

40. Which attribute gives the memory layout information of a NumPy array?

a) arr.flags
b) arr.layout
c) arr.memory
d) arr.buffer
Correct Answer: a) arr.flags
Explanation:
The flags attribute returns information about the memory layout of the array (e.g., C_CONTIGUOUS).

41. What does np.full(shape, fill_value) do?

a) Creates an array filled with zeros
b) Creates an array of the given shape filled with fill_value
c) Resizes an array to full capacity
d) Fills missing values in an array
Correct Answer: b> Creates an array of the given shape filled with fill_value
Explanation:
np.full() returns a new array of given shape and type, filled with fill_value.

42. Which NumPy function computes the cumulative sum of array elements over a given axis?

a) np.sum()
b) np.cumsum()
c) np.add.accumulate()
d) np.running_sum()
Correct Answer: b) np.cumsum()
Explanation:
np.cumsum() returns the cumulative sum of the elements along a given axis.

43. What is the output of np.array([1, 2, 3]) 2?

a) [1, 4, 9]
b) [2, 4, 6]
c) [1, 8, 27]
d) Error
Correct Answer: a) [1, 4, 9]
Explanation:
Exponentiation in NumPy is also performed element-wise.

44. Which method inserts an item at a specified index in Python's built-in 'array' module?

a) add()
b) insert()
c) push()
d) append()
Correct Answer: b) insert()
Explanation:
The insert() method inserts a new item before the specified index in the array.

45. What does np.argsort() return?

a) The sorted array elements
b) The indices that would sort an array
c) A boolean sorting mask
d) The rank of elements
Correct Answer: b) The indices that would sort an array
Explanation:
np.argsort() returns the indices that would sort an array along a given axis.

46. Which function stacks arrays horizontally (column wise) in NumPy?

a) np.vstack()
b) np.hstack()
c) np.dstack()
d) np.row_stack()
Correct Answer: b) np.hstack()
Explanation:
np.hstack() stacks arrays in sequence horizontally (column wise).

47. What is the default data type when creating a NumPy array of integers on a 64-bit system?

a) int32
b) int64
c) float64
d) int16
Correct Answer: b) int64
Explanation:
On 64-bit platforms, the default integer data type for NumPy arrays is int64.

48. Which function computes the matrix determinant in NumPy's linear algebra package?

a) np.linalg.det()
b) np.det()
c) np.matrix.det()
d) np.linalg.determinant()
Correct Answer: a) np.linalg.det()
Explanation:
np.linalg.det() computes the determinant of an array matrix.

49. What does np.diag() do when passed a 2D array?

a) Extracts the diagonal elements
b) Replaces diagonal elements with zeros
c) Transposes the matrix
d) Inverts the diagonal
Correct Answer: a) Extracts the diagonal elements
Explanation:
When given a 2D array, np.diag() extracts and returns its diagonal elements as a 1D array.

50. Which Python module contains the array type for compact numeric arrays?

a) array
b) list
c) collections
d) itertools
Correct Answer: a) array
Explanation:
The built-in 'array' module provides compact array objects for uniform primitive types.

51. What is the output of np.round([1.2, 2.5, 3.8])?

a) [1.0, 2.0, 4.0]
b) [1.0, 2.0, 3.0]
c) [1.0, 3.0, 4.0]
d) [2.0, 3.0, 4.0]
Correct Answer: a) [1.0, 2.0, 4.0]
Explanation:
np.round() rounds elements to the nearest integer (using round-to-even for .5 values, so 2.5 rounds to 2.0 or bankersen's rounding depending on configuration; wait, numpy uses round-to-even: 2.5 rounds to 2.0, 3.8 rounds to 4.0, let's verify standard numpy round behavior: np.round(2.5) is 2.0).

52. Which function calculates the exponential of all elements in a NumPy array?

a) np.exp()
b) np.power()
c) np.log()
d) np.pow()
Correct Answer: a) np.exp()
Explanation:
np.exp() calculates the exponential element-wise for all elements in the array.

53. What does the .data attribute of a NumPy array point to?

a) A Python list of the data
b) A memory buffer containing the actual elements of the array
c) The metadata dictionary
d) The shape tuple
Correct Answer: b) A memory buffer containing the actual elements of the array
Explanation:
The data attribute is the memory buffer pointing to the start of the array's data.

54. Which function generates values linearly interpolated between two points?

a) np.linspace()
b) np.arange()
c) np.interpolate()
d) np.geomspace()
Correct Answer: a) np.linspace()
Explanation:
np.linspace creates specified number of samples evenly spaced over a specified interval.

55. What is the time complexity of searching for an element by value in an unsorted array or list?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Correct Answer: b) O(n)
Explanation:
Linear search through an unsorted collection takes O(n) time complexity.

56. Which NumPy function returns the indices where elements are non-zero?

a) np.nonzero()
b) np.where()
c) np.find()
d) np.indices()
Correct Answer: a) np.nonzero()
Explanation:
np.nonzero() returns the indices of the elements that are non-zero.

57. What does np.clip(arr, a_min, a_max) do?

a) Removes elements outside the range [a_min, a_max]
b) Limits the values in an array so that they fall between a_min and a_max
c) Slices the array between a_min and a_max indices
d) Sorts the array within the limits
Correct Answer: b) Limits the values in an array so that they fall between a_min and a_max
Explanation:
np.clip() clips (limits) the values in an array, setting anything below a_min to a_min and above a_max to a_max.

58. Which function computes the inverse of a square matrix in NumPy?

a) np.linalg.inv()
b) np.inverse()
c) np.matrix.inv()
d) np.inv()
Correct Answer: a) np.linalg.inv()
Explanation:
np.linalg.inv() computes the multiplicative inverse of a matrix.

59. What is the output of np.array([1, 2]) + 5?

a) [6, 7]
b) Error
c) [1, 2, 5]
d) [5, 10]
Correct Answer: a) [6, 7]
Explanation:
NumPy supports scalar broadcasting, adding the scalar 5 to each element of the array.

60. Which method is used to save a NumPy array to a binary file in NumPy's native format?

a) np.save()
b) np.write()
c) np.dump()
d) np.store()
Correct Answer: a) np.save()
Explanation:
np.save() saves an array to a binary file in NumPy .npy format.

61. What does np.load() do?

a) Loads a saved NumPy array from a .npy or .npz file
b) Loads a module into memory
c) Reads a standard text file into a list
d) Initializes a blank array
Correct Answer: a) Loads a saved NumPy array from a .npy or .npz file
Explanation:
np.load() is used to read arrays saved with np.save() or np.savez().

62. Which function calculates the cross product of two vectors in NumPy?

a) np.dot()
b) np.cross()
c) np.multiply()
d) np.outer()
Correct Answer: b) np.cross()
Explanation:
np.cross() returns the cross product of two vectors.

63. What is the purpose of the order='C' parameter in NumPy array creation?

a) Specifies C-style row-major memory layout
b) Specifies Fortran-style column-major memory layout
c) Compresses the array using C algorithms
d) Converts elements to C data types
Correct Answer: a) Specifies C-style row-major memory layout
Explanation:
order='C' means the array is stored in row-major order (C-style).

64. Which function calculates the variance of a NumPy array?

a) np.std()
b) np.var()
c) np.mean()
d) np.average()
Correct Answer: b) np.var()
Explanation:
np.var() computes the variance along the specified axis.

65. What does np.squeeze() do?

a) Removes single-dimensional entries from the shape of an array
b) Compresses the array size in memory
c) Flattens the entire array to 1D
d) Truncates decimal values
Correct Answer: a) Removes single-dimensional entries from the shape of an array
Explanation:
np.squeeze() removes axes of length 1 from the array shape.

66. Which attribute of a NumPy array returns the number of bytes used by a single item?

a) itemsize
b) nbytes
c) size
d) bytesize
Correct Answer: a) itemsize
Explanation:
itemsize returns the length of one array element in bytes.

67. What is the output of np.ceil([1.2, 2.7, 3.1])?

a) [1.0, 2.0, 3.0]
b) [2.0, 3.0, 4.0]
c) [1.0, 3.0, 4.0]
d) [2.0, 2.0, 4.0]
Correct Answer: b) [2.0, 3.0, 4.0]
Explanation:
np.ceil() returns the ceiling of the input, element-wise (smallest integer greater than or equal to each element).

68. Which function creates a 2D array with ones on the diagonal and zeros elsewhere, with a specified diagonal offset?

a) np.eye()
b) np.identity()
c) np.diagflat()
d) np.ones()
Correct Answer: a) np.eye()
Explanation:
np.eye() allows specifying an index offset (k parameter) for the diagonal.

69. What does np.floor([1.2, 2.7, 3.1]) return?

a) [1.0, 2.0, 3.0]
b) [2.0, 3.0, 4.0]
c) [1.0, 3.0, 4.0]
d) [1.0, 2.0, 4.0]
Correct Answer: a) [1.0, 2.0, 3.0]
Explanation:
np.floor() returns the floor of the input, element-wise (largest integer less than or equal to each element).

70. Which method of Python's built-in array module returns the current buffer address and length?

a) buffer_info()
b) address_info()
c) memory_info()
d) get_buffer()
Correct Answer: a) buffer_info()
Explanation:
buffer_info() returns a tuple (address, length) giving the current memory address and element count.

71. What is the output of np.isnan(np.nan)?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
np.isnan() tests element-wise for NaN (Not a Number) and returns True for np.nan.

72. Which function computes the outer product of two vectors in NumPy?

a) np.outer()
b) np.dot()
c) np.inner()
d) np.kron()
Correct Answer: a) np.outer()
Explanation:
np.outer() computes the outer product of two vectors.

73. What does the np.isscalar() function check?

a) Whether an element is a scalar value
b) Whether an array is 1D
c) Whether data is numeric
d) Whether the array size is zero
Correct Answer: a) Whether an element is a scalar value
Explanation:
np.isscalar() tests whether the type of a given object is a scalar type.

74. Which function sorts a NumPy array and returns a sorted copy?

a) np.sort()
b) arr.sort()
c) np.order()
d) np.arrange()
Correct Answer: a) np.sort()
Explanation:
np.sort() returns a sorted copy of an array, whereas arr.sort() sorts the array in-place.

75. What does the arr.sort() method do?

a) Sorts the array in-place
b) Returns a sorted copy
c) Sorts in descending order by default
d) Raises an error
Correct Answer: a) Sorts the array in-place
Explanation:
arr.sort() sorts the NumPy array in-place, modifying the original array.

76. Which function computes the eigenvalues and eigenvectors of a square matrix?

a) np.linalg.eig()
b) np.linalg.eigen()
c) np.eig()
d) np.eigenvalues()
Correct Answer: a) np.linalg.eig()
Explanation:
np.linalg.eig() computes eigenvalues and eigenvectors of a square matrix.

77. What is the purpose of np.expand_dims(arr, axis)?

a) Expands the shape of an array by adding a new axis of size 1
b) Resizes the array to a larger dimension
c) Extends array elements
d) Pads the array with zeros
Correct Answer: a) Expands the shape of an array by adding a new axis of size 1
Explanation:
np.expand_dims() introduces a new axis at the specified position, increasing the dimensions by one.

78. Which function returns the indices of the minimum values along an axis?

a) np.argmin()
b) np.min()
c) np.minimum()
d) np.findmin()
Correct Answer: a) np.argmin()
Explanation:
np.argmin() returns the indices of the minimum values along a given axis.

79. What does np.inf represent in NumPy?

a) Positive infinity
b) Invalid number
c) Integer overflow limit
d) Infinite loop flag
Correct Answer: a) Positive infinity
Explanation:
np.inf is a floating-point representation of positive infinity.

80. Which function calculates the natural logarithm element-wise?

a) np.log()
b) np.ln()
c) np.log10()
d) np.log2()
Correct Answer: a) np.log()
Explanation:
np.log() calculates the natural logarithm (base e) element-wise.

81. What does np.copy(arr) ensure?

a) A deep copy of the array and its data in memory
b) A shared view of the array
c) A reference copy
d) An immutable version of the array
Correct Answer: a) A deep copy of the array and its data in memory
Explanation:
np.copy() creates an explicit copy of the array data, ensuring modifications do not affect the original.

82. Which function calculates the sum of all elements in a NumPy array?

a) np.sum()
b) np.total()
c) np.add()
d) np.aggregate()
Correct Answer: a) np.sum()
Explanation:
np.sum() returns the sum of array elements over a given axis.

83. What is the output of np.isinf(np.inf)?

a) True
b) False
c) None
d) Error
Correct Answer: a) True
Explanation:
np.isinf() tests element-wise for positive or negative infinity, returning True for np.inf.

84. Which function computes the inner product of two vectors?

a) np.inner()
b) np.dot()
c) np.cross()
d) np.multiply()
Correct Answer: a) np.inner()
Explanation:
np.inner() computes the inner product of two vectors.

85. What does the typecode property return on an array object from Python's built-in array module?

a) The single character type code used to create the array
b) The string name of the data type
c) The size in bytes
d) The format string
Correct Answer: a) The single character type code used to create the array
Explanation:
The typecode attribute returns the character type code (e.g., 'i', 'f', 'd') used when initializing the array.

86. Which function computes the absolute values of elements in an array?

a) np.abs()
b) np.absolute()
c) Both np.abs() and np.absolute()
d) np.fabs()
Correct Answer: c) Both np.abs() and np.absolute()
Explanation:
Both np.abs() and np.absolute() calculate the absolute value element-wise.

87. What is the purpose of np.linspace(start, stop, num)

a) Generates num evenly spaced samples over the specified interval [start, stop]
b) Generates numbers with a step size of num
c) Generates random numbers between start and stop
d) Creates logarithmic partitions
Correct Answer: a) Generates num evenly spaced samples over the specified interval [start, stop]
Explanation:
np.linspace returns num evenly spaced samples, calculated over the interval [start, stop].

88. Which function solves a linear matrix equation, or system of linear scalar equations?

a) np.linalg.solve()
b) np.solve()
c) np.matrix.solve()
d) np.linsolve()
Correct Answer: a) np.linalg.solve()
Explanation:
np.linalg.solve() solves the linear equation system ax = b.

89. What does np.ma module stand for in NumPy?

a) Masked Arrays
b) Matrix Arithmetic
c) Memory Allocation
d) Multidimensional Algorithms
Correct Answer: a) Masked Arrays
Explanation:
np.ma provides support for masked arrays, which can handle missing or invalid data.

90. Which function returns the indices that would partition an array?

a) np.argpartition()
b) np.partition()
c) np.argsort()
d) np.index()
Correct Answer: a) np.argpartition()
Explanation:
np.argpartition() performs an indirect partition using the algorithms specified by kind.

91. What is the output of np.prod([1, 2, 3, 4])?

a) 24
b) 10
c) 12
d) 0
Correct Answer: a) 24
Explanation:
np.prod() returns the product of array elements over a given axis (1 * 2 * 3 * 4 = 24).

92. Which function calculates the rank (number of dimensions) of a matrix in NumPy's linear algebra module?

a) np.linalg.matrix_rank()
b) np.rank()
c) np.linalg.rank()
d) np.matrix.rank()
Correct Answer: a) np.linalg.matrix_rank()
Explanation:
np.linalg.matrix_rank() returns matrix rank using SVD matrix decomposition.

93. What does np.trim_zeros() do?

a) Trims the leading and/or trailing zeros from a 1D array or sequence
b) Removes all zeros from a multidimensional array
c) Replaces zero values with NaNs
d) Deletes empty array rows
Correct Answer: a) Trims the leading and/or trailing zeros from a 1D array or sequence
Explanation:
np.trim_zeros() trims leading and trailing zeros from a 1D array or sequence.

94. Which function generates a random sample from a given 1D array?

a) np.random.choice()
b) np.random.sample()
c) np.random.pick()
d) np.random.select()
Correct Answer: a) np.random.choice()
Explanation:
np.random.choice() generates a random sample from a given 1D array.

95. What is the return type of np.where() condition matching?

a) A tuple of arrays, one for each dimension of the array
b) A single list of indices
c) A boolean mask
d) An integer count
Correct Answer: a) A tuple of arrays, one for each dimension of the array
Explanation:
When called without x and y, np.where(condition) returns a tuple of arrays, one for each dimension, containing the indices where condition is true.
← Previous: Latest Python Operators MCQs
Next →: Python Control Flow MCQs
NewPython OOP MCQs

Python OOP MCQs

Object-Oriented Programming (OOP) in Python is a programming paradigm that uses classes and objects to model real world entities, promoting…

By MCQs Generator
NewPython Control Flow MCQs

Python Control Flow MCQs

Control flow statements regulate the order in which individual code statements are evaluated and executed in a Python program. Python…

By MCQs Generator
Newpython variables & datatypes MCQs

Python Variables & Data Types MCQs

Variables in Python act as dynamic references reserved in memory to store objects, operating without explicit data type declarations due…

By MCQs Generator