Bird
0
0

Find the error in this code:

medium📝 Debug Q6 of 15
NumPy - Creating Arrays
Find the error in this code:
import numpy as np
lst = [1, 2, 3]
arr = np.array(lst)
print(arr[3])
AIndexError: index 3 is out of bounds
BSyntaxError: invalid syntax
CTypeError: list indices must be integers
DNo error, prints 3
Step-by-Step Solution
Solution:
  1. Step 1: Check array length

    The array has elements at indices 0, 1, 2 only.
  2. Step 2: Accessing index 3

    Accessing index 3 is out of bounds and causes IndexError.
  3. Final Answer:

    IndexError: index 3 is out of bounds -> Option A
  4. Quick Check:

    Array indices start at 0, max index is length-1 [OK]
Quick Trick: Array indices go from 0 to length-1 [OK]
Common Mistakes:
  • Accessing index equal to length
  • Confusing syntax error with index error
  • Assuming Python lists and numpy arrays behave differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes