Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q14 of 15
NumPy - Indexing and Slicing
What is wrong with this code snippet?
arr = np.array([1,2,3,4,5])
slice = arr[4:1:-1]

What will slice contain?
A[4 3 2]
B[5 4 3]
CEmpty array
DIndexError
Step-by-Step Solution
Solution:
  1. Step 1: Understand negative step slicing

    With step=-1, slicing goes backwards from start index 4 down to stop index 1 (exclusive).
  2. Step 2: Identify elements selected

    Indexes selected: 4 (5), 3 (4), 2 (3). So slice contains [5 4 3].
  3. Final Answer:

    [5 4 3] -> Option B
  4. Quick Check:

    Backward slice from 4 to 2 = [5 4 3] [OK]
Quick Trick: Negative step slices backward, stop is exclusive [OK]
Common Mistakes:
  • Expecting forward slice with negative step
  • Including stop index element
  • Thinking it causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes