Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Fundamentals

What will be the output of the following code?

import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr.shape)
A(3, 2)
B(2, 2, 2)
C(2, 4)
D(4, 2)
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the array structure

    The array is a 3D array with two main elements, each containing two sub-elements, each of which contains two numbers.
  2. Step 2: Determine the shape

    So, the shape corresponds to (2 blocks, 2 rows, 2 columns) = (2, 2, 2).
  3. Final Answer:

    (2, 2, 2) -> Option B
  4. Quick Check:

    Count elements in each dimension [OK]
Quick Trick: Count nested brackets to find shape dimensions [OK]
Common Mistakes:
  • Confusing total elements with shape
  • Miscounting nested list levels
  • Assuming 2D shape for 3D array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes