Exercise: Python

Questions for: Arrays

Given two NumPy arrays arr1 and arr2, what does np.column_stack((arr1, arr2)) do?
A:
Stacks the arrays vertically.
B:
Stacks the arrays horizontally.
C:
Creates a new array by vertically concatenating the arrays.
D:
Creates a new array by horizontally concatenating the arrays.
Answer: B
The np.column_stack() function stacks the arrays horizontally.
How can you reshape a NumPy array arr into a 2x3 matrix?
A:
arr.reshape(2, 3)
B:
arr.resize(2, 3)
C:
np.reshape(arr, (2, 3))
D:
np.resize(arr, (2, 3))
Answer: A
The arr.reshape(2, 3) method reshapes the array into a 2x3 matrix.
What does the NumPy function numpy.concatenate((arr1, arr2), axis=0) do?
A:
Joins the arrays along the specified axis.
B:
Adds the arrays element-wise.
C:
Multiplies the arrays element-wise.
D:
Computes the cross product of the arrays.
Answer: A
The numpy.concatenate() function joins the arrays along the specified axis.
How do you check if all elements in a NumPy array arr are non-zero?
A:
np.check_nonzero(arr)
B:
np.nonzero(arr)
C:
np.all_nonzero(arr)
D:
np.all(arr != 0)
Answer: D
The code np.all(arr != 0) checks if all elements in the array are non-zero.
Given a NumPy array arr = np.array([3, 1, 4, 1, 5, 9]), what does the code arr[::-1] do?
A:
Reverses the order of elements in the array.
B:
Selects only the even-indexed elements.
C:
Creates a new array with the same elements.
D:
Removes the last element from the array.
Answer: A
The code arr[::-1] reverses the order of elements in the array.
Ad Slot (Above Pagination)
Quiz