Exercise: Python

Questions for: Arrays

Given a NumPy array arr = np.array([3, 1, 4, 1, 5, 9]), what does np.sort(arr) return?
A:
[1, 1, 3, 4, 5, 9]
B:
[9, 5, 4, 3, 1, 1]
C:
[1, 3, 1, 4, 5, 9]
D:
[1, 1, 4, 3, 5, 9]
Answer: A
The np.sort() function returns a sorted version of the array.
How can you find the index of the minimum value in a NumPy array arr?
A:
np.argmin(arr)
B:
arr.index(min(arr))
C:
np.minimum_index(arr)
D:
arr.min_index()
Answer: A
The np.argmin() function returns the index of the minimum value in the array.
What does the NumPy function numpy.random.rand(2, 3) do?
A:
Creates a 2x3 array with all elements initialized to zero.
B:
Generates a random array with values between 0 and 1.
C:
Reshapes an existing array to have dimensions 2x3.
D:
Computes the square root of each element in the array.
Answer: B
The numpy.random.rand() function generates a random array with values between 0 and 1.
How can you calculate the element-wise product of two NumPy arrays arr1 and arr2?
A:
np.multiply(arr1, arr2)
B:
arr1 * arr2
C:
np.prod(arr1, arr2)
D:
np.product(arr1, arr2)
Answer: B
The element-wise product can be calculated using the multiplication operator *.
What is the output of the code np.arange(2, 10, 2)?
A:
[2, 4, 6, 8]
B:
[2, 4, 8]
C:
[2, 6]
D:
[2, 4, 8, 10]
Answer: A
The code generates an array with values from 2 to 10 (exclusive) with a step of 2.
Ad Slot (Above Pagination)
Quiz