Bird
0
0

Identify the error in this code:

medium📝 Debug Q7 of 15
NumPy - Fundamentals
Identify the error in this code:
import numpy as np
lst = [1, 2, 3]
arr = np.array(lst)
print(arr + [1, 2])
ACannot add arrays of different shapes without broadcasting
BNumPy arrays cannot be added to lists
CSyntax error in array creation
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check shapes of operands

    arr has shape (3,), list [1, 2] has length 2.
  2. Step 2: Broadcasting rules

    Shapes are incompatible for broadcasting addition.
  3. Step 3: Result

    This causes a ValueError due to shape mismatch.
  4. Final Answer:

    Cannot add arrays of different shapes without broadcasting -> Option A
  5. Quick Check:

    Shape mismatch causes addition error [OK]
Quick Trick: Shapes must align or broadcast for addition [OK]
Common Mistakes:
  • Assuming NumPy arrays cannot add lists
  • Ignoring shape mismatch
  • Thinking syntax error causes failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes