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([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])
A[10 30 50]
B[20 40 60]
C[10 20 30]
D[50 40 30]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the array and indices

    arr contains [10, 20, 30, 40, 50]. Indices are [0, 2, 4].
  2. Step 2: Select elements at given indices

    Elements at positions 0, 2, and 4 are 10, 30, and 50 respectively.
  3. Final Answer:

    [10 30 50] -> Option A
  4. Quick Check:

    arr[[0,2,4]] = [10 30 50] [OK]
Quick Trick: Index list picks elements in order given [OK]
Common Mistakes:
  • Mixing up indices and values
  • Assuming output is sorted
  • Confusing with slicing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes