Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q4 of 15
NumPy - Indexing and Slicing
What is the output of the following code?
import numpy as np
arr = np.array([1, 4, 7, 10])
result = np.where(arr > 5, arr, 0)
print(result)
A[7 10 0 0]
B[0 0 7 10]
C[1 4 0 0]
D[1 4 7 10]
Step-by-Step Solution
Solution:
  1. Step 1: Check condition arr > 5

    Elements greater than 5 are 7 and 10 at indices 2 and 3.
  2. Step 2: Apply np.where to replace others with 0

    Elements not greater than 5 (1 and 4) become 0, others stay the same.
  3. Final Answer:

    [0 0 7 10] -> Option B
  4. Quick Check:

    np.where(condition, arr, 0) replaces False with 0 [OK]
Quick Trick: np.where replaces False with third argument [OK]
Common Mistakes:
  • Confusing which elements are replaced
  • Expecting original array unchanged
  • Mixing up indices and values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes