NumPy MCQ
1). What is the purpose of the np.random.rand() function?
a) Generates a random integer
b) Returns a random permutation of an array
c) Generates an array of random floats in the range [0, 1)
d) Calculates the cumulative sum of an array
Correct answer is: c) Generates an array of random floats in the range [0, 1)
Explanation: The np.random.rand() function generates an array of random floats in the half-open interval [0, 1). It takes the desired shape of the array as arguments.
2). How can you calculate the element-wise arccosine of a NumPy array?
a) np.arccos(arr)
b) np.arcsin(arr)
c) np.arctan(arr)
d) np.arccosh(arr)
Correct answer is: a) np.arccos(arr)
Explanation: The np.arccos() function in NumPy calculates the element-wise arccosine (inverse cosine) of a given array. It takes the array as an argument and returns a new array with the arccosine values.
3). What does the np.delete() function do?
a) Deletes an element from an array
b) Deletes a row or column from a 2-dimensional array
c) Deletes multiple elements from an array
d) Deletes a subarray from an array
Correct answer is: b) Deletes a row or column from a 2-dimensional array
Explanation: The np.delete() function in NumPy deletes a specified row or column from a 2-dimensional array. It takes the array and the index of the row or column to delete as arguments.
4). Which NumPy function is used to calculate the element-wise absolute difference between two arrays?
a) np.absdiff()
b) np.subtract()
c) np.diff()
d) np.absolute_diff()
Correct answer is: a) np.absdiff()
Explanation: The np.absdiff() 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.
5). What does the np.stack() function do?
a) Combines multiple arrays into a single array
b) Splits an array into multiple subarrays
c) Joins arrays along a new axis
d) Reverses the order of elements in an array
Correct answer is: c) Joins arrays along a new axis
Explanation: The np.stack() function in NumPy joins arrays along a new axis. It takes the arrays to stack as arguments and returns a new array with the arrays stacked along the specified axis.
6). How can you calculate the element-wise tangent of a NumPy array?
a) np.tan(arr)
b) np.arctan(arr)
c) np.sin(arr)
d) np.cos(arr)
Correct answer is: a) np.tan(arr)
Explanation: The np.tan() function in NumPy calculates the element-wise tangent of a given array. It takes the array as an argument and returns a new array with the tangent values.
7). What is the purpose of the np.full() function?
a) Generates an array filled with ones
b) Generates an array filled with zeros
c) Generates an array with a specified shape and fill value
d) Generates an array with random values
Correct answer is: c) Generates an array with a specified shape and fill value
Explanation: The np.full() function in NumPy generates an array with a specified shape and fill value. It takes the desired shape and the fill value as arguments and returns an array with the specified shape, filled with the fill value.
8). Which NumPy function is used to calculate the element-wise hyperbolic sine of an array?
a) np.sinh()
b) np.cosh()
c) np.tanh()
d) np.arcsinh()
Correct answer is: a) np.sinh()
Explanation: The np.sinh() function in NumPy calculates the element-wise hyperbolic sine of a given array. It takes the array as an argument and returns a new array with the hyperbolic sine values.
9). What does the np.argsort() function return?
a) The sorted array
b) The indices that would sort the array
c) The indices that sort the array in descending order
d) The indices of the maximum values in the array
Correct answer is: b) The indices that would sort the array
Explanation: The np.argsort() function returns the indices that would sort an array in ascending order. The initial array is unaltered.
10). How can you calculate the element-wise bitwise AND of two NumPy arrays?
a) np.bitwise_and(arr1, arr2)
b) np.bitwise_or(arr1, arr2)
c) np.bitwise_xor(arr1, arr2)
d) np.bitwise_not(arr1)
Correct answer is: a) np.bitwise_and(arr1, arr2)
Explanation: The np.bitwise_and() function in NumPy calculates the element-wise bitwise AND of two arrays. It takes the two arrays as arguments and returns a new array with the bitwise AND result.
11). What does the np.isinf() function do?
a) Checks if an array contains infinite values
b) Computes the element-wise exponential of an array
c) Calculates the cumulative sum of an array
d) Calculates the element-wise reciprocal of an array
Correct answer is: a) Checks if an array contains infinite values
Explanation: The np.isinf() function in NumPy checks if an array contains infinite values. It returns a Boolean array indicating which elements are infinite.
12). Which NumPy function is used to calculate the element-wise square root of an array?
a) np.square_root()
b) np.power(arr, 0.5)
c) np.sqrt(arr)
d) np.abs_sqrt(arr)
Correct answer is: c) np.sqrt(arr)
Explanation: The np.sqrt() function in NumPy calculates the element-wise square root of a given array. It takes the array as an argument and returns a new array with the square root values.
13). What is the purpose of the np.rot90() function?
a) It rotates an array by 90 degrees counter-clockwise
b) It rotates an array by 90 degrees clockwise
c) It reshapes an array to a specified shape
d) It transposes the rows and columns of an array
Correct answer is: a) It rotates an array by 90 degrees counter-clockwise
Explanation: The np.rot90() function in NumPy rotates an array by 90 degrees counter-clockwise. It takes the array and the number of rotations as arguments and returns a new array with the specified rotation.
14). Which NumPy function is used to calculate the element-wise exponential of an array?
a) np.exp()
b) np.power()
c) np.log()
d) np.square()
Correct answer is: a) np.exp()
Explanation: The np.exp() function in NumPy calculates the element-wise exponential of a given array. It takes the array as an argument and returns a new array with the exponential values.
15). What does the np.median() function return?
a) The mean of the array
b) The maximum value in the array
c) The minimum value in the array
d) The median of the array
Correct answer is: d) The median of the array
Explanation: The np.median() function in NumPy calculates the median value of an array. It takes the array as an argument and returns the median value.
16). How can you calculate the element-wise absolute difference between two NumPy arrays?
a) np.absdiff(arr1, arr2)
b) np.subtract(arr1, arr2)
c) np.add(arr1, arr2)
d) np.multiply(arr1, arr2)
Correct answer is: a) np.absdiff(arr1, arr2)
Explanation: The np.absdiff() 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.
17). Which NumPy function is used to calculate the element-wise arc tangent of two arrays?
a) np.arctan2()
b) np.arcsin()
c) np.arccos()
d) np.tan()
Correct answer is: a) np.arctan2()
Explanation: The np.arctan2() function in NumPy calculates the element-wise arc tangent of the quotient of two arrays. It takes the numerator array and the denominator array as arguments and returns a new array with the arc tangent values.
18). What is the purpose of the np.fliplr() function?
a) It flips the array vertically
b) It flips the array horizontally
c) It changes the shape of the array
d) It transposes the rows and columns of the array
Correct answer is: b) It flips the array horizontally
Explanation: The np.fliplr() function in NumPy flips the array horizontally. It takes the array as an argument and returns a new array with the columns reversed.
19). Which NumPy function is used to calculate the element-wise hyperbolic tangent of an array?
a) np.tanh()
b) np.sinh()
c) np.cosh()
d) np.arctanh()
Correct answer is: a) np.tanh()
Explanation: The np.tanh() function in NumPy calculates the element-wise hyperbolic tangent of a given array. It takes the array as an argument and returns a new array with the hyperbolic tangent values.
20). What does the np.vstack() function do?
a) Vertically stacks multiple arrays
b) Horizontally stacks multiple arrays
c) Splits an array into multiple subarrays
d) Calculates the dot product of two arrays
Correct answer is: a) Vertically stacks multiple arrays
Explanation: The np.vstack() function in NumPy vertically stacks multiple arrays. It takes the arrays to stack as arguments and returns a new array with the arrays stacked vertically.
21). What does the np.full_like() function do?
a) Creates a new array with the same shape and data type as a specified array, filled with a specified value
b) Creates a new array with the same shape as a specified array, filled with a specified value
c) Creates a new array with the same data type as a specified array, filled with a specified value
d) Creates a new array with the same shape and data type as a specified array, filled with ones
Correct answer is: a) Creates a new array with the same shape and data type as a specified array, filled with a specified value
Explanation: The np.full_like() function in NumPy creates a new array with the same shape and data type as a specified array. It takes the specified array and the fill value as arguments and returns a new array filled with the specified value.
22). How can you calculate the element-wise bitwise OR of two NumPy arrays?
a) np.bitwise_or(arr1, arr2)
b) np.bitwise_and(arr1, arr2)
c) np.bitwise_xor(arr1, arr2)
d) np.bitwise_not(arr1)
Correct answer is: a) np.bitwise_or(arr1, arr2)
Explanation: The np.bitwise_or() function in NumPy calculates the element-wise bitwise OR of two arrays. It takes the two arrays as arguments and returns a new array with the bitwise OR result.
23). What is the purpose of the np.linspace() function?
a) Generates a sequence of evenly spaced numbers over a specified range
b) Generates an array with random values
c) Generates an array filled with ones
d) Generates an array filled with zeros
Correct answer is: a) Generates a sequence of evenly spaced numbers over a specified range
Explanation: The np.linspace() function in NumPy generates a sequence of evenly spaced numbers over a specified range. It takes the start value, end value, and the number of elements as arguments.
24). What does the np.ptp() function calculate?
a) The sum of all elements in the array
b) The median of the array
c) The peak-to-peak (range) value of the array
d) The array’s total product of all of its elements
Correct answer is: c) The peak-to-peak (range) value of the array
Explanation: The np.ptp() function in NumPy calculates the peak-to-peak (range) value of an array. It takes the array as an argument and returns the difference between the maximum and minimum values.
25). Which NumPy function is used to calculate the element-wise sigmoid function of an array?
a) np.sinh()
b) np.cosh()
c) np.tanh()
d) np.expit()
Correct answer is: d) np.expit()
Explanation: The np.expit() function in NumPy calculates the element-wise sigmoid function of a given array. It takes the array as an argument and returns a new array with the sigmoid values.