NumPy MCQs with Answers

20+ NumPy MCQs with Answers

1. What is NumPy?

  • A Python library for scientific computing with support for large, multi-dimensional arrays and matrices.
  • A Python library for web development with support for creating and managing websites.
  • A Python library for creating GUI applications with support for graphical user interface elements.
  • A Python library for creating 3D graphics with support for rendering 3D objects and scenes.

Answer: a) NumPy is a Python library for scientific computing with support for large, multi-dimensional arrays and matrices.

2. What is the correct way to create a NumPy array with elements 1, 2, 3?

  • np.array([1,2,3])
  • np.array(1,2,3)
  • np.array((1,2,3))
  • All of the above

Answer: a) The correct way to create a NumPy array with elements 1, 2, 3 is np.array([1,2,3]).

3. Which of the following NumPy array operations will return a new array with the same shape as the original array, but with all elements set to 0?

  • np.zeros_like()
  • np.zeros()
  • np.empty()
  • np.ones()

Answer: a) np.zeros_like() will return a new array with the same shape as the original array, but with all elements set to 0.

4. What is the difference between np.ndarray.shape and np.ndarray.size?

  • np.ndarray.shape returns the number of elements in the array, while np.ndarray.size returns the shape of the array.
  • np.ndarray.shape returns the shape of the array, while np.ndarray.size returns the number of dimensions of the array.
  • np.ndarray.shape returns the shape of the array, while np.ndarray.size returns the number of elements in the array.
  • np.ndarray.shape and np.ndarray.size are the same thing.

Answer: c) np.ndarray.shape returns the shape of the array, while np.ndarray.size returns the number of elements in the array.

5. Which NumPy function is used to sort a given array?

  • np.sort()
  • np.unique()
  • np.argmax()
  • np.add()

Answer: a) np.sort() is used to sort a given array.

6. What is the difference between np.array() and np.asarray()?

  • np.array() creates a new array and copies the data, while np.asarray() creates a new view of the array.
  • np.array() creates a new view of the array, while np.asarray() creates a new array and copies the data.
  • np.array() and np.asarray() are the same thing.
  • np.array() and np.asarray() both create a new view of the array.

Answer: a) np.array() creates a new array and copies the data, while np.asarray() creates a new view of the array.

7. Which NumPy function is used to find the maximum element in a given array?

  • np.max()
  • np.mean()
  • np.sum()
  • np.prod()

Answer: a) np.max() is used to find the maximum element in a given array.

8. What is the output of the following code snippet?

import numpy as np
 
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)
  • 2
  • 3
  • 4
  • 5

Answer: a) The output of the code snippet is 2, which represents the number of dimensions in the array.

9. What is broadcasting in NumPy?

  • A technique that allows arrays with different shapes to be used together in arithmetic operations.
  • A method of resizing arrays to match the shape of another array.
  • A way to perform element-wise operations on two arrays with the same shape.
  • A function that returns a new array with the same shape as the input array, but with all elements set to 0.

Answer: a) Broadcasting is a technique that allows arrays with different shapes to be used together in arithmetic operations.

10. Which of the following is a NumPy function used for linear algebra?

  • np.sin()
  • np.exp()
  • np.linalg.det()
  • np.abs()

Answer: c) np.linalg.det() is a NumPy function used for calculating the determinant of a matrix in linear algebra.

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

  • np.dot()
  • np.cross()
  • np.transpose()
  • np.ravel()

Answer: a) np.dot() is used to compute the dot product of two arrays.

12. What is the difference between np.reshape() and np.resize()?

  • np.reshape() creates a new view of the array, while np.resize() resizes the original array.
  • np.reshape() resizes the original array, while np.resize() creates a new view of the array.
  • np.reshape() and np.resize() are the same thing.
  • np.reshape() and np.resize() both create a new view of the array.

Answer: a) np.reshape() creates a new view of the array, while np.resize() resizes the original array.

13. Which of the following is not a valid way to create a NumPy array?

  • np.array([1, 2, 3])
  • np.arange(10)
  • np.linspace(0, 1, 5)
  • np.matrix([1, 2, 3])

Answer: d) np.matrix([1, 2, 3]) is not a valid way to create a NumPy array. It creates a matrix object, which is not the same as a NumPy array.

14. What is the output of the following code snippet?

import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a + b
print(c)
  • [5, 7, 9]
  • [4, 5, 6, 1, 2, 3]
  • [4, 7, 10]
  • [1, 2, 3, 4, 5, 6]

Answer: c) The output of the code snippet is [5, 7, 9], which represents the element-wise sum of the two arrays.

15. Which NumPy function is used to compute the mean of an array?

  • np.mean()
  • np.sum()
  • np.prod()
  • np.std()

Answer: a) np.mean() is used to compute the mean of an array.

16. Which of the following is not a valid NumPy data type?

  • int64
  • float32
  • complex64
  • string

Answer: d) string is not a valid NumPy data type. The string data type is supported in Python's built-in string module.

17. What is the output of the following code snippet?

import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.dot(a, b)
print(c)
  • [5, 7, 9]
  • [4, 5, 6, 1, 2, 3]
  • [32]
  • [1, 2, 3, 4, 5, 6]

Answer: c) The output of the code snippet is [32], which represents the dot product of the two arrays.

18. Which NumPy function is used to compute the standard deviation of an array?

  • np.mean()
  • np.sum()
  • np.prod()
  • np.std()

Answer: d) np.std() is used to compute the standard deviation of an array.

19. Which NumPy function is used to create a new array with a specified shape and type filled with zeros?

  • np.ones()
  • np.zeros()
  • np.full()
  • np.empty()

Answer: b) np.zeros() is used to create a new array with a specified shape and type filled with zeros.

20. What is the output of the following code snippet?

import numpy as np
 
a = np.array([1, 2, 3, 4])
b = a.reshape(2, 2)
print(b)
  • [[1, 2], [3, 4]]
  • [[1, 2, 3, 4]]
  • [[1], [2], [3], [4]]
  • [[1], [2, 3], [4]]

Answer: a) The output of the code snippet is [[1, 2], [3, 4]], which represents the reshaped array with two rows and two columns.

21. Which of the following is not a valid NumPy array indexing method?

  • Integer indexing
  • Slicing
  • Boolean indexing
  • Character indexing

Answer: d) Character indexing is not a valid NumPy array indexing method.

22. What is the output of the following code snippet?

import numpy as np
 
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.concatenate((a, b))
print(c)
  • [1, 2, 3, 4, 5, 6]
  • [[1, 2, 3], [4, 5, 6]]
  • [[1, 4], [2, 5], [3, 6]]
  • [4, 5, 6, 1, 2, 3]

Answer: a) The output of the code snippet is [1, 2, 3, 4, 5, 6], which represents the concatenated array of a and b.

23. Which NumPy function is used to compute the element-wise product of two arrays?

  • np.multiply()
  • np.add()
  • np.subtract()
  • np.divide()

Answer: a) np.multiply() is used to compute the element-wise product of two arrays.

5/5 (1)

Similar Posts