Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Creating Arrays
What is the output of the following code?
import numpy as np
arr = np.ones((2, 3), dtype=int)
print(arr)
A[[1. 1. 1.] [1. 1. 1.]]
B[[1 1 1] [1 1 1]]
C[[0 0 0] [0 0 0]]
D[[1 0 1] [0 1 0]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand np.ones() with dtype=int

    The function creates a 2x3 array filled with ones as integers.
  2. Step 2: Check the printed output

    Since dtype is int, the ones appear as 1 without decimal points, arranged in 2 rows and 3 columns.
  3. Final Answer:

    [[1 1 1] [1 1 1]] -> Option B
  4. Quick Check:

    np.ones() with dtype=int = integer ones array [OK]
Quick Trick: dtype=int makes ones appear as integers, no decimals [OK]
Common Mistakes:
  • Expecting float ones when dtype=int is set
  • Confusing zeros with ones
  • Misreading array shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes