CARVIEW |
Shaping and Reshaping in NumPy
This quiz contains 10 questions, progressing from basic to advanced levels of shaping and reshaping arrays. Each question includes the correct answer and a detailed justification.
Question 1
What does the shape attribute of a NumPy array return?
The total number of elements in the array
The dimensions of the array
A flattened version of the array
The data type of the array elements
Question 2
Which of the following statements is true about the reshape() function?
It changes the array shape in place.
The total number of elements must remain the same after reshaping.
It only works for 1D arrays.
It always requires the order argument.
Question 3
What will be the output of the following code?
import numpy as np
arr = np.arange(6)
print(arr.reshape(2, 3))
[[1 2 3]
[4 5 6]]
[[0 1 2]
[3 4 5]]
[0 1 2 3 4 5]
Error
Question 4
What does the np.ravel() function do?
Converts a 2D array into a 1D array
Changes the shape of an array permanently
Adds an axis to the array
None of the Above
Question 5
What will happen if you try to reshape an array with incompatible dimensions?
It will pad the array with zeros.
It will raise a ValueError.
It will automatically adjust the dimensions.
It will silently fail and return the original array.
Question 6
What is the effect of setting one dimension as -1 in reshape()?
It transposes the array.
It allows NumPy to automatically calculate the size of that dimension.
It flattens the array.
It throws an error.
Question 7
What will be the shape of the following array after reshaping?
import numpy as np
arr = np.arange(12)
new_arr = arr.reshape(3, -1)
print(new_arr.shape)
(3, 4)
(4, 3)
(3, 12)
Error
Question 8
Which function is used to add new dimensions to an array?
np.expand_dims()
np.newaxis
Both a and b
np.concatenate()
Question 9
What will be the output of the following code?
import numpy as np
arr = np.array([1, 2, 3])
new_arr = np.expand_dims(arr, axis=1)
print(new_arr.shape)
(3, )
(1, 3)
(3, 1)
Error
Question 10
What will the following code output?
import numpy as np
arr = np.arange(24).reshape(2, 3, 4)
transposed = np.transpose(arr, (1, 0, 2))
print(transposed.shape)
(2, 3, 4)
(3, 2, 4)
Error
(4, 3, 2)
There are 10 questions to complete.