Bird
0
0

What is the shape and type of the output from this code?

medium📝 Predict Output Q13 of 15
NumPy - Creating Arrays
What is the shape and type of the output from this code?
import numpy as np
arr = np.random.rand(2, 4)
print(arr.shape, type(arr))
A(4, 2) <class 'numpy.ndarray'>
B(2, 4) <class 'list'>
C(2, 4) <class 'numpy.ndarray'>
D(4, 2) <class 'list'>
Step-by-Step Solution
Solution:
  1. Step 1: Understand np.random.rand(2, 4) output

    This creates a NumPy array with shape (2, 4) filled with random floats.
  2. Step 2: Check the type of the variable arr

    It is a NumPy ndarray, not a Python list.
  3. Final Answer:

    (2, 4) <class 'numpy.ndarray'> -> Option C
  4. Quick Check:

    Shape and type match NumPy array output [OK]
Quick Trick: np.random.rand dims = shape; output is always ndarray [OK]
Common Mistakes:
  • Confusing shape order (2,4) vs (4,2)
  • Thinking output is a Python list
  • Misreading print output format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes