Exercise: Python

Questions for: Arrays

How do you initialize an array with five zeros using the array module?
A:
arr = array(5, 0)
B:
arr = array.zeros(5)
C:
arr = array([0, 0, 0, 0, 0])
D:
arr = array.array('i', [0, 0, 0, 0, 0])
Answer: D
The correct way to initialize an array with five zeros using the array module is array.array('i', [0, 0, 0, 0, 0]).
What is the purpose of the numpy.flatten() method?
A:
Reshapes the array.
B:
Converts the array to a list.
C:
Computes the sum of the array elements.
D:
Retrieves the maximum value in the array.
Answer: B
The numpy.flatten() method flattens the array, converting it to a one-dimensional list.
How do you perform element-wise addition of two NumPy arrays, arr1 and arr2?
A:
arr1.add(arr2)
B:
np.add(arr1, arr2)
C:
arr1 + arr2
D:
np.sum(arr1, arr2)
Answer: C
The + operator can be used for element-wise addition of two NumPy arrays.
What is the purpose of the numpy.shape attribute in a NumPy array?
A:
Retrieves the length of the array.
B:
Returns the number of dimensions in the array.
C:
Reshapes the array.
D:
Converts the array to a list.
Answer: B
The numpy.shape attribute returns a tuple representing the dimensions of the NumPy array.
How do you create a 2D NumPy array?
A:
np.array([[1, 2, 3], [4, 5, 6]])
B:
numpy.create_2d_array([[1, 2, 3], [4, 5, 6]])
C:
array.create([[1, 2, 3], [4, 5, 6]])
D:
np.array([1, 2, 3], [4, 5, 6])
Answer: A
The correct syntax to create a 2D NumPy array is np.array([[1, 2, 3], [4, 5, 6]]).
Ad Slot (Above Pagination)
Quiz