Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Indexing and Slicing
What will be the output of this code?
import numpy as np
arr = np.array([5, 10, 15])
result = np.where(arr % 2 == 0, 'even', 'odd')
print(result)
A['even' 'odd' 'even']
B['odd' 'odd' 'odd']
C[5 10 15]
D['odd' 'even' 'odd']
Step-by-Step Solution
Solution:
  1. Step 1: Check condition arr % 2 == 0

    Only 10 is even, 5 and 15 are odd.
  2. Step 2: np.where assigns 'even' or 'odd'

    Positions with True get 'even', others get 'odd'.
  3. Final Answer:

    ['odd' 'even' 'odd'] -> Option D
  4. Quick Check:

    np.where(condition, 'even', 'odd') labels parity [OK]
Quick Trick: Use np.where for labeling based on condition [OK]
Common Mistakes:
  • Mixing up even and odd labels
  • Expecting numeric output instead of strings
  • Confusing modulo condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes