Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
NumPy - Indexing and Slicing
What is wrong with this code?
import numpy as np
arr = np.array([10, 20, 30])
print(arr[1:5])
ANo error; prints elements from index 1 to end
BIndexError because 5 is out of bounds
CSyntaxError due to slice syntax
DPrints empty array
Step-by-Step Solution
Solution:
  1. Step 1: Understand slicing beyond array length

    Slicing with end index beyond array length does not cause error; it stops at array end.
  2. Step 2: Identify output of arr[1:5]

    Elements from index 1 to end are [20, 30], so these print.
  3. Final Answer:

    No error; prints elements from index 1 to end -> Option A
  4. Quick Check:

    Slicing past end = no error, returns available elements [OK]
Quick Trick: Slicing past array end is safe, returns available elements [OK]
Common Mistakes:
  • Expecting IndexError on slice
  • Confusing slice with single index
  • Thinking slice syntax is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes