Bird
0
0

What is the output of the following code?

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

    Elements greater than 25 are 30 and 40 at indices 2 and 3.
  2. Step 2: Apply np.where() selection

    For elements > 25, keep original value; else replace with 0. So result is [0, 0, 30, 40].
  3. Final Answer:

    [ 0 0 30 40] -> Option A
  4. Quick Check:

    Condition true keeps value, false replaces with 0 [OK]
Quick Trick: Check condition element-wise, replace false with second array [OK]
Common Mistakes:
  • Confusing which elements satisfy the condition
  • Mixing up true and false array values
  • Forgetting np.where returns a new array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes