NumPy MCQ : Set 1

NumPy MCQ

1). What is NumPy?

a) A programming language
b) A numerical computing library for Python
c) A data visualization library
d) A machine learning framework

Correct answer is: b) A numerical computing library for Python
Explanation: NumPy is a powerful library for numerical computing in Python. It offers support for sizable, multidimensional arrays and matrices, as well as a range of mathematical operations for effectively using these arrays.

2). Which of the following is not a key feature of NumPy?

a) N-dimensional array object
b) Broadcasting functions
c) Linear algebra routines
d) Data visualization tools

Correct answer is: d) Data visualization tools
Explanation: While NumPy provides efficient numerical computing capabilities, it does not include built-in data visualization tools. For data visualization, libraries like Matplotlib or Seaborn are commonly used in conjunction with NumPy.

3). What is the main benefit of using NumPy arrays over Python lists?

a) NumPy arrays can store elements of different data types
b) NumPy arrays consume less memory
c) NumPy arrays can have variable lengths
d) NumPy arrays support dynamic resizing

Correct answer is: b) NumPy arrays consume less memory
Explanation: NumPy arrays are more memory-efficient compared to Python lists because they store elements of the same data type in a contiguous block of memory. This allows for better performance and reduced memory consumption.

4). Which of the following statements is true about NumPy arrays?

a) NumPy arrays are immutable
b) NumPy arrays can have varying lengths
c) NumPy arrays can only store numeric data types
d) NumPy arrays are resizable

Correct answer is: c) NumPy arrays can only store numeric data types
Explanation: NumPy arrays are designed to work with homogeneous data, meaning they can only store elements of a single data type, such as integers, floats, or complex numbers.

5). How can you create a NumPy array from a Python list?

a) np.array(list_name)
b) numpy.array(list_name)
c) np.ndarray(list_name)
d) numpy.ndarray(list_name)

Correct answer is: a) np.array(list_name)
Explanation: The `np.array()` function in NumPy can be used to create an array from a Python list. It accepts the argument list and returns a NumPy array.

6). Which NumPy function is used to calculate the mean of an array?

a) np.mean()
b) np.average()
c) np.median()
d) np.sum()

Correct answer is: a) np.mean()
Explanation: The `np.mean()` function in NumPy is used to calculate the arithmetic mean of an array. It takes the array as an argument and returns the mean value.

7). Which NumPy function is used to find the maximum value in an array?

a) np.max()
b) np.maximum()
c) np.argmax()
d) np.amax()

Correct answer is: a) np.max()
Explanation: The `np.max()` function is used to find the maximum value in a NumPy array. It takes the array as an argument and returns the maximum value.

8). How can you reshape a 1-dimensional array into a 2-dimensional array with two rows?

a) arr.reshape((2, 2))
b) arr.reshape((2, -1))
c) arr.reshape((1, 2))
d) arr.reshape((2,))

Correct answer is: b) arr.reshape((2, -1))
Explanation: The `reshape()` function in NumPy can be used to change the shape of an array. By specifying the target shape as `(2, -1)`, NumPy automatically infers the number of columns based on the size of the array.

9). What does the `np.linspace()` function do?

a) Generates a sequence of equally spaced values
b) Returns a random permutation of an array
c) Calculates the logarithm of each element in an array
d) Performs element-wise multiplication of two arrays

Correct answer is: a) Generates a sequence of equally spaced values
Explanation: The `np.linspace()` function generates a sequence of equally spaced values over a specified interval. It takes the start, end, and the number of values to generate as arguments.

10). How can you concatenate two NumPy arrays vertically?

a) np.concatenate((arr1, arr2), axis=0)
b) np.vstack((arr1, arr2))
c) np.append(arr1, arr2, axis=0)
d) All of the above

Correct answer is: d) All of the above
Explanation: All the provided options can be used to concatenate two NumPy arrays vertically, resulting in an array that has the rows from both arrays stacked on top of each other.

11). What does the `np.random.randint()` function do?

a) Generates a random integer
b) Returns a random permutation of an array
c) Generates an array of random integers
d) Calculates the cumulative sum of an array

Correct answer is: c) Generates an array of random integers
Explanation: The `np.random.randint()` function generates an array of random integers from a specified low (inclusive) to high (exclusive) range. The function takes the low, high, and the size of the array as arguments.

12). Which NumPy function is used to compute the dot product of two arrays?

a) np.dot()
b) np.cross()
c) np.inner()
d) np.outer()

Correct answer is: a) np.dot()
Explanation: The `np.dot()` function in NumPy is used to compute the dot product of two arrays. It performs matrix multiplication if the inputs are 2-dimensional arrays.

13). What is the purpose of broadcasting in NumPy?

a) To perform element-wise operations on arrays of different shapes
b) To adjust the shape of an array to match another array
c) To randomly shuffle the elements of an array
d) To compute the cumulative sum of an array

Correct answer is: a) To perform element-wise operations on arrays of different shapes
Explanation: Broadcasting in NumPy allows for performing element-wise operations on arrays of different shapes by automatically adjusting their shapes to be compatible.

14). How can you save a NumPy array to a file?

a) np.save(file_name, arr)
b) np.savez(file_name, arr)
c) np.savetxt(file_name, arr)
d) All of the above

Correct answer is: d) All of the above
Explanation: All the provided options can be used to save a NumPy array to a file. `np.save()` saves the array in binary format, `np.savez()` saves multiple arrays in an uncompressed archive, and `np.savetxt()` saves the array as a text file.

15). How can you load a NumPy array from a file?

a) np.load(file_name)
b) np.loadtxt(file_name)
c) np.fromfile(file_name)
d) All of the above

Correct answer is: a) np.load(file_name)
Explanation: The `np.load()` function in NumPy is used to load a saved NumPy array from a file. It takes the file name as an argument and returns the loaded array.

16). Which NumPy function is used to calculate the standard deviation of an array?

a) np.std()
b) np.var()
c) np.mean()
d) np.median()

Correct answer is: a) np.std()
Explanation: The `np.std()` function in NumPy calculates the standard deviation of an array, which measures the spread of values around the mean. It takes the array as an argument and returns the standard deviation.

17). What does the `np.eye()` function do?

a) Generates a diagonal array
b) Returns the element-wise exponential of an array
c) Calculates the inverse of a matrix
d) Performs element-wise division of two arrays

Correct answer is: a) Generates a diagonal array
Explanation: A two-dimensional array with ones on the diagonal and zeros elsewhere is produced using the ‘np.eye()’ function. It takes the number of rows as an argument, and by default, it creates a square array.

18). Which NumPy function is used to find the indices of the sorted elements in an array?

a) np.sort()
b) np.argsort()
c) np.sort_indices()
d) np.searchsorted()

Correct answer is: b) np.argsort()
Explanation: The `np.argsort()` function in NumPy returns the indices that would sort an array in ascending order. It takes the array as an argument and returns an array of indices.

19). What is the purpose of the `axis` parameter in NumPy functions?

a) It specifies the data type of the array
b) It controls the dimensionality of the array
c) It determines the axis along which the operation is applied
d) It defines the shape of the resulting array

Correct answer is: c) It determines the axis along which the operation is applied
Explanation: The `axis` parameter in NumPy functions is used to specify the axis along which the operation is applied. It can be used to perform operations across rows, columns, or other dimensions of the array.

20). How can you calculate the element-wise square root of a NumPy array?

a) np.sqrt(arr)
b) np.square(arr)
c) np.power(arr, 0.5)
d) All of the above

Correct answer is: a) 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 of each element.

21). What does the `np.transpose()` function do?

a) Flips the array vertically
b) Flips the array horizontally
c) Changes the shape of the array
d) Interchanges the rows and columns of the array

Correct answer is: d) Interchanges the rows and columns of the array
Explanation: The `np.transpose()` function in NumPy returns a view of the array with the rows and columns interchanged. The initial array is unaltered.

22). What is the purpose of the `np.meshgrid()` function?

a) It generates a grid of coordinates based on input arrays
b) It computes the element-wise product of two arrays
c) It calculates the dot product of two arrays
d) It reshapes an array into a specified shape

Correct answer is: a) It generates a grid of coordinates based on input arrays
Explanation: The `np.meshgrid()` function is used to generate a grid of coordinates based on the input arrays. It takes two or more arrays representing the coordinates and returns coordinate matrices for each pair of input arrays.

23). Which NumPy function is used to calculate the element-wise absolute value of an array?

a) np.abs()
b) np.absolute()
c) np.absolutes()
d) np.magnitude()

Correct answer is: a) np.abs()
Explanation: The `np.abs()` function in NumPy calculates the element-wise absolute value of a given array. It takes the array as an argument and returns a new array with the absolute values.

24). What does the `np.histogram()` function do?

a) Calculates a data set’s histogram
b) Generates a random array with a specified shape
c) Calculates the cumulative sum of an array
d) Performs element-wise multiplication of two arrays

Correct answer is: a) Calculates a data set’s histogram
Explanation: The `np.histogram()` function computes the histogram of a set of data. It takes the data and the number of bins as arguments and returns an array of bin counts and an array of bin edges.

25). How can you calculate the element-wise exponential of a NumPy array?

a) np.exp(arr)
b) np.power(arr, 2)
c) np.log(arr)
d) np.sin(arr)

Correct answer is: a) np.exp(arr)
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.

Leave a Comment