Python Programming - Arrays

Exercise : Arrays - General Questions
  • Arrays - General Questions
11.
What is the purpose of the numpy.shape attribute in a NumPy array?
Retrieves the length of the array.
Returns the number of dimensions in the array.
Reshapes the array.
Converts the array to a list.
Answer: Option
Explanation:
The numpy.shape attribute returns a tuple representing the dimensions of the NumPy array.

12.
How do you perform element-wise addition of two NumPy arrays, arr1 and arr2?
arr1.add(arr2)
np.add(arr1, arr2)
arr1 + arr2
np.sum(arr1, arr2)
Answer: Option
Explanation:
The + operator can be used for element-wise addition of two NumPy arrays.

13.
What is the purpose of the numpy.flatten() method?
Reshapes the array.
Converts the array to a list.
Computes the sum of the array elements.
Retrieves the maximum value in the array.
Answer: Option
Explanation:
The numpy.flatten() method flattens the array, converting it to a one-dimensional list.

14.
How do you initialize an array with five zeros using the array module?
arr = array(5, 0)
arr = array.zeros(5)
arr = array([0, 0, 0, 0, 0])
arr = array.array('i', [0, 0, 0, 0, 0])
Answer: Option
Explanation:
The correct way to initialize an array with five zeros using the array module is array.array('i', [0, 0, 0, 0, 0]).

15.
What is the purpose of the array.remove(element) method in the array module?
Removes the first occurrence of the specified element from the array.
Removes all occurrences of the specified element from the array.
Removes the element at the specified index.
Raises a ValueError.
Answer: Option
Explanation:
The remove() method in the array module removes the first occurrence of the specified element from the array.