Python NumPy MCQ Questions and Answers

1. What is NumPy mainly used for in Python?

a) Web development
b) Data manipulation and analysis
c) Machine learning
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

NumPy is a fundamental package for scientific computing in Python, widely used for data manipulation, analysis, and machine learning.

2. How do you create a NumPy array from a Python list?

a) numpy.create_array(list)
b) numpy.array(list)
c) numpy.list_to_array(list)
d) numpy.make_array(list)

Answer:

b) numpy.array(list)

Explanation:

NumPy arrays can be created from Python lists using the numpy.array() function.

3. What does the 'shape' attribute of a NumPy array represent?

a) The size of the array
b) The data type of the array
c) The dimensions of the array
d) The total number of elements in the array

Answer:

c) The dimensions of the array

Explanation:

The 'shape' attribute of a NumPy array indicates the dimensions of the array, represented as a tuple of integers.

4. How do you initialize a 3×3 matrix with zeros using NumPy?

a) numpy.zeros(3, 3)
b) numpy.zeros((3, 3))
c) numpy.array([3, 3])
d) numpy.matrix.zeros(3, 3)

Answer:

b) numpy.zeros((3, 3))

Explanation:

The numpy.zeros() function creates an array filled with zeros. The shape of the array is specified as a tuple (3, 3) for a 3×3 matrix.

5. Which function is used to perform element-wise addition of two NumPy arrays?

a) numpy.add()
b) numpy.sum()
c) numpy.concatenate()
d) numpy.append()

Answer:

a) numpy.add()

Explanation:

The numpy.add() function is used to perform element-wise addition of two arrays.

6. What is the output of numpy.arange(5)?

a) An array from 0 to 5
b) An array from 1 to 5
c) An array from 0 to 4
d) An error

Answer:

c) An array from 0 to 4

Explanation:

numpy.arange(5) generates an array with elements from 0 to 4 (5 not included).

7. How do you generate a random number between 0 and 1 in NumPy?

a) numpy.random()
b) numpy.random.rand()
c) numpy.rand()
d) numpy.random.random()

Answer:

b) numpy.random.rand()

Explanation:

The numpy.random.rand() function generates a random number between 0 and 1.

8. What does the 'dtype' attribute in a NumPy array describe?

a) The dimension of the array
b) The size of the array
c) The data type of the array elements
d) The shape of the array

Answer:

c) The data type of the array elements

Explanation:

The 'dtype' attribute specifies the data type of the elements in the NumPy array.

9. How do you compute the dot product of two NumPy arrays, 'a' and 'b'?

a) numpy.dot(a, b)
b) a.dot(b)
c) numpy.product(a, b)
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

The dot product can be computed using numpy.dot(a, b) or a.dot(b) in NumPy.

10. How do you create an identity matrix of size 3×3 in NumPy?

a) numpy.identity(3)
b) numpy.eye(3)
c) numpy.id_matrix(3)
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

Both numpy.identity(3) and numpy.eye(3) can be used to create a 3×3 identity matrix in NumPy.

11. What is the purpose of numpy.linspace(start, stop, num)?

a) To create an array of 'num' evenly spaced values between 'start' and 'stop'
b) To create an array of 'num' random values between 'start' and 'stop'
c) To create a logarithmically spaced array
d) To create an array of 'num' elements from 'start' to 'stop'

Answer:

a) To create an array of 'num' evenly spaced values between 'start' and 'stop'

Explanation:

numpy.linspace() generates an array of 'num' evenly spaced samples, calculated over the interval [start, stop].

12. How do you find the maximum value in a NumPy array 'a'?

a) a.max()
b) numpy.max(a)
c) max(a)
d) Both a) and b)

Answer:

d) Both a) and b)

Explanation:

You can use either a.max() or numpy.max(a) to find the maximum value in a NumPy array.

13. What does numpy.reshape() do?

a) Changes the shape of an array without changing its data
b) Resizes the array to a new shape
c) Transposes the array
d) Flattens the array to one dimension

Answer:

a) Changes the shape of an array without changing its data

Explanation:

numpy.reshape() gives a new shape to an array without changing the data.

14. What is slicing in NumPy?

a) Cutting elements from an array based on index
b) Sorting elements in an array
c) Removing elements from an array
d) Dividing an array into multiple smaller arrays

Answer:

a) Cutting elements from an array based on index

Explanation:

Slicing in NumPy is selecting specific elements or a range of elements from an array.

15. How do you reverse the elements of a NumPy array 'a'?

a) a.reverse()
b) a[::-1]
c) numpy.flip(a)
d) Both b) and c)

Answer:

d) Both b) and c)

Explanation:

You can reverse the elements of a NumPy array either by using slicing a[::-1] or the numpy.flip() function.

16. How do you check the version of NumPy installed in your environment?

a) numpy.__version__
b) numpy.version
c) numpy.get_version()
d) numpy.version_info

Answer:

a) numpy.__version__

Explanation:

The numpy.__version__ attribute shows the version of NumPy installed in your environment.

17. How do you create a NumPy array with only even numbers from 2 to 10?

a) numpy.arange(2, 11, 2)
b) numpy.array([2, 4, 6, 8, 10])
c) numpy.linspace(2, 10, 5)
d) numpy.evens(2, 10)

Answer:

a) numpy.arange(2, 11, 2)

Explanation:

numpy.arange(2, 11, 2) creates an array of even numbers starting from 2 to 10 with a step of 2.

18. What is broadcasting in NumPy?

a) Sending data over a network
b) Expanding smaller arrays to align with larger arrays
c) Converting arrays to a common type
d) Broadcasting data to multiple arrays

Answer:

b) Expanding smaller arrays to align with larger arrays

Explanation:

Broadcasting in NumPy refers to the ability of the library to treat arrays of different shapes during arithmetic operations.

19. How do you create a 2D array (3×4) filled with random integers between 0 and 10 in NumPy?

a) numpy.random.randint(0, 10, (3, 4))
b) numpy.random(0, 10, size=(3, 4))
c) numpy.random_array(3, 4, 0, 10)
d) numpy.integers(0, 10, (3, 4))

Answer:

a) numpy.random.randint(0, 10, (3, 4))

Explanation:

The numpy.random.randint() function generates random integers, and the shape of the array is specified by the size parameter.

20. What does numpy.vstack() do?

a) Stacks arrays in sequence vertically (row-wise)
b) Stacks arrays in sequence horizontally (column-wise)
c) Stacks arrays in depth
d) Transposes the stacked arrays

Answer:

a) Stacks arrays in sequence vertically (row-wise)

Explanation:

numpy.vstack() stacks arrays vertically, meaning it stacks them in rows, one on top of the other.

21. How do you normalize a NumPy array?

a) (array – array.min()) / (array.max() – array.min())
b) array / array.max()
c) array – array.mean()
d) numpy.normalize(array)

Answer:

a) (array – array.min()) / (array.max() – array.min())

Explanation:

Normalizing an array involves scaling the values to a range of [0, 1], which can be done by subtracting the minimum and dividing by the range.

22. What does numpy.hsplit() do?

a) Splits an array into multiple sub-arrays horizontally
b) Splits an array into multiple sub-arrays vertically
c) Splits an array into multiple sub-arrays along the third axis
d) Splits an array into multiple equal parts

Answer:

a) Splits an array into multiple sub-arrays horizontally

Explanation:

numpy.hsplit() is used to split an array into multiple sub-arrays horizontally (column-wise).

23. How do you calculate the mean along the column in a 2D NumPy array 'a'?

a) a.mean(axis=0)
b) a.mean(axis=1)
c) numpy.mean(a)
d) Both a) and c)

Answer:

a) a.mean(axis=0)

Explanation:

To calculate the mean along each column in a 2D NumPy array, use a.mean(axis=0). The axis parameter specifies the axis along which the means are computed.

24. What is the use of numpy.where() function?

a) To find the index of an element
b) To replace elements based on a condition
c) To merge two arrays
d) To sort an array

Answer:

b) To replace elements based on a condition

Explanation:

The numpy.where() function is used to return elements chosen from two arrays or from the array itself, based on a specified condition.

25. How do you calculate the eigenvalues and eigenvectors of a square matrix 'A' in NumPy?

a) numpy.linalg.eigen(A)
b) numpy.eigen(A)
c) numpy.linalg.eig(A)
d) A.eigenvalues()

Answer:

c) numpy.linalg.eig(A)

Explanation:

The numpy.linalg.eig() function computes the eigenvalues and right eigenvectors of a square matrix 'A'.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top