NumPy MCQ
1). What does the `np.unique()` function do?
a) Removes duplicate elements from an array
b) Returns the element-wise square of an array
c) Computes the cumulative sum of an array
d) Performs element-wise division of two arrays
Correct answer is: a) Removes duplicate elements from an array
Explanation: The `np.unique()` function in NumPy returns the sorted, unique values in an array, removing any duplicate elements.
2). How can you calculate the element-wise sine of a NumPy array?
a) np.sin(arr)
b) np.cos(arr)
c) np.tan(arr)
d) All of the above
Correct answer is: a) np.sin(arr)
Explanation: The `np.sin()` function in NumPy calculates the element-wise sine of a given array. It takes the array as an argument and returns a new array with the sine values.
3). Which NumPy function is used to compute the covariance matrix of a set of variables?
a) np.cov()
b) np.correlate()
c) np.corrcoef()
d) np.corr()
Correct answer is: a) np.cov()
Explanation: The `np.cov()` function in NumPy is used to compute the covariance matrix of a set of variables. It takes the variables as arguments and returns the covariance matrix.
4). What does the `np.concatenate()` function do?
a) Combines multiple arrays into a single array
b) Splits an array into multiple subarrays
c) Reverses the order of elements in an array
d) Sorts the elements of an array in ascending order
Correct answer is: a) Combines multiple arrays into a single array
Explanation: The `np.concatenate()` function in NumPy is used to combine multiple arrays along a specified axis, resulting in a single array.
5). How can you calculate the element-wise logarithm of a NumPy array?
a) np.log(arr)
b) np.exp(arr)
c) np.power(arr, 2)
d) np.sqrt(arr)
Correct answer is: a) np.log(arr)
Explanation: The `np.log()` function in NumPy calculates the element-wise natural logarithm (base e) of a given array. It takes the array as an argument and returns a new array with the logarithmic values.
6). What is the purpose of the `np.where()` function?
a) It finds the maximum value in an array
b) It replaces elements in an array based on a condition
c) It calculates the element-wise sum of two arrays
d) It arranges array elements in decreasing order.
Correct answer is: b) It replaces elements in an array based on a condition
Explanation: The `np.where()` function in NumPy is used to replace elements in an array based on a specified condition. It takes the condition, the values to replace when the condition is True, and the values to replace when the condition is False.
7). What does the `np.pad()` function do?
a) Adds padding to an array
b) Removes padding from an array
c) Changes the shape of an array
d) Calculates the element-wise difference of two arrays
Correct answer is: a) Adds padding to an array
Explanation: The `np.pad()` function in NumPy is used to add padding to an array. It takes the array, the pad widths, and the padding mode as arguments, and returns a new array with the specified padding.
8). How can you calculate the element-wise product of two NumPy arrays?
a) np.multiply(arr1, arr2)
b) np.dot(arr1, arr2)
c) np.cross(arr1, arr2)
d) np.divide(arr1, arr2)
Correct answer is: a) np.multiply(arr1, arr2)
Explanation: The `np.multiply()` function in NumPy calculates the element-wise product of two arrays. It takes the two arrays as arguments and returns a new array with the product of corresponding elements.
9). What does the `np.nan` value represent in NumPy?
a) Not a number
b) Null or missing value
c) Infinite value
d) Negative infinity
Correct answer is: a) Not a number
Explanation: The `np.nan` value in NumPy represents a “Not a Number” value, typically used to denote missing or undefined data.
10). Which NumPy function is used to calculate the element-wise absolute difference between two arrays?
a) np.abs()
b) np.diff()
c) np.subtract()
d) np.absolute()
Correct answer is: d) np.absolute()
Explanation: The `np.absolute()` function in NumPy calculates the element-wise absolute difference between two arrays. It takes the two arrays as arguments and returns a new array with the absolute differences.
11). What is the purpose of the `np.clip()` function?
a) It rounds the elements of an array to the nearest integer
b) It limits the values of an array to a specified range
c) It calculates the cumulative product of an array
d) It computes the element-wise exponential of an array
Correct answer is: b) It limits the values of an array to a specified range
Explanation: The `np.clip()` function in NumPy limits the values of an array to a specified range. It takes the array, the minimum value, and the maximum value as arguments, and returns a new array with the values clipped to the specified range.
12). How can you calculate the element-wise division of two NumPy arrays?
a) np.divide(arr1, arr2)
b) np.multiply(arr1, arr2)
c) np.power(arr1, arr2)
d) np.dot(arr1, arr2)
Correct answer is: a) np.divide(arr1, arr2)
Explanation: The `np.divide()` function in NumPy calculates the element-wise division of two arrays. It takes the two arrays as arguments and returns a new array with the quotient of corresponding elements.
13). What does the `np.around()` function do?
a) The array’s elements are rounded to the nearest integer.
b) Rounds the elements of an array to a specified number of decimals
c) Returns the integer part of the elements in an array
d) Calculates the element-wise absolute value of an array
Correct answer is: b) Rounds the elements of an array to a specified number of decimals
Explanation: The `np.around()` function in NumPy rounds the elements of an array to a specified number of decimals. It takes the array and the number of decimals as arguments and returns a new array with the rounded values.
14). Which NumPy function is used to calculate the element-wise arctangent of an array?
a) np.arctan()
b) np.arcsin()
c) np.arccos()
d) np.arctan2()
Correct answer is: a) np.arctan()
Explanation: The `np.arctan()` function in NumPy calculates the element-wise arctangent (inverse tangent) of a given array. It takes the array as an argument and returns a new array with the arctangent values.
15). What is the purpose of the `np.isclose()` function?
a) It checks if two arrays are element-wise equal
b) It calculates the element-wise sum of two arrays
c) It computes the element-wise exponential of an array
d) It rounds the elements of an array to the nearest integer
Correct answer is: a) It checks if two arrays are element-wise equal
Explanation: The `np.isclose()` function in NumPy checks if two arrays are element-wise equal within a specified tolerance. It returns a Boolean array indicating the element-wise equality.
16). How can you calculate the element-wise power of a NumPy array?
a) np.power(arr, exponent)
b) np.sqrt(arr)
c) np.abs(arr)
d) np.log(arr)
Correct answer is: a) np.power(arr, exponent)
Explanation: The `np.power()` function in NumPy calculates the element-wise power of a given array with a specified exponent. It takes the array and the exponent as arguments and returns a new array with the powered values.
17). What does the `np.mean()` function return if the array contains NaN values?
a) NaN
b) 0
c) The mean of the non-NaN values
d) An error is raised
Correct answer is: c) The mean of the non-NaN values
Explanation: The `np.mean()` function ignores NaN values when calculating the mean of an array. It returns the mean of the non-NaN values in the array.
18). Which NumPy function is used to calculate the element-wise cosine of an array?
a) np.sin()
b) np.cos()
c) np.tan()
d) np.arccos()
Correct answer is: b) np.cos()
Explanation: The `np.cos()` function in NumPy calculates the element-wise cosine of a given array. It takes the array as an argument and returns a new array with the cosine values.
19). What is the purpose of the `np.zeros()` function?
a) It generates an array filled with zeros
b) It generates an array filled with ones
c) It generates an array filled with random values
d) It generates an array with a specified shape and values
Correct answer is: a) It generates an array filled with zeros
Explanation: The `np.zeros()` function in NumPy generates an array filled with zeros. It takes the desired shape of the array as an argument and returns an array with the specified shape and filled with zeros.
20). Which of the following functions is used to find the variance of an array?
a) var()
b) variance()
c) numpy.var()
d) numpy.variance()
Correct answer is: c) numpy.var()
Explanation: The var() function is a NumPy function that returns the variance of an array. The variance() function is a Python function that returns the variance of a sequence. The numpy.var() and numpy.variance() functions are equivalent.
21). How can you calculate the element-wise floor division of two NumPy arrays?
a) np.floor_divide(arr1, arr2)
b) np.divide(arr1, arr2)
c) np.multiply(arr1, arr2)
d) np.subtract(arr1, arr2)
Correct answer is: a) np.floor_divide(arr1, arr2)
Explanation: The `np.floor_divide()` function in NumPy calculates the element-wise floor division of two arrays. It takes the two arrays as arguments and returns a new array with the quotient of corresponding elements, rounded down to the nearest integer.
22). Which NumPy function is used to calculate the element-wise logarithm base 10 of an array?
a) np.log10()
b) np.log2()
c) np.log()
d) np.log1p()
Correct answer is: a) np.log10()
Explanation: The np.log10() function in NumPy calculates the element-wise logarithm base 10 of a given array. It takes the array as an argument and returns a new array with the logarithm values.
23). What does the np.ravel() function do?
a) Reshapes an array to a specified shape
b) Returns a sorted copy of an array
c) Flattens an array into a 1-dimensional array
d) Rounds the elements of an array to the nearest integer
Correct answer is: c) Flattens an array into a 1-dimensional array
Explanation: The np.ravel() function in NumPy flattens an array into a 1-dimensional array by concatenating all elements. It returns a new array with the flattened structure.
24). How can you calculate the element-wise square of a NumPy array?
a) np.square(arr)
b) np.power(arr, 2)
c) np.sqrt(arr)
d) np.abs(arr)
Correct answer is: a) np.square(arr)
Explanation: The np.square() function in NumPy calculates the element-wise square of a given array. It takes the array as an argument and returns a new array with the squared values.
25). Which NumPy function is used to calculate the element-wise inverse of an array?
a) np.inverse()
b) np.reciprocal()
c) np.divide(1, arr)
d) np.divide(arr, 1)
Correct answer is: b) np.reciprocal()
Explanation: The np.reciprocal() function in NumPy calculates the element-wise reciprocal (inverse) of a given array. It takes the array as an argument and returns a new array with the reciprocal values.