Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
NumPy - Creating Arrays
What will be the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
A(4,)
B(1, 4)
C(2, 2)
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand the array structure

    The array is created from a list of two lists, each with 2 elements, so it is 2 rows and 2 columns.
  2. Step 2: Check the shape attribute

    arr.shape returns the dimensions as a tuple (rows, columns), which is (2, 2).
  3. Final Answer:

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

    2 rows and 2 columns shape = (2, 2) [OK]
Quick Trick: Shape shows rows and columns count as tuple [OK]
Common Mistakes:
  • Confusing shape with size
  • Expecting a flat tuple for nested lists
  • Thinking shape returns error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes