Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q6 of 15
NumPy - Fundamentals
Find the error in this code snippet:
import numpy as np
list1 = [1, 2, 3]
arr1 = np.array(list1)
print(arr1 + 5)
ACannot add scalar to array
BNo error, scalar addition works element-wise
CMust convert 5 to array first
DSyntax error in array creation
Step-by-Step Solution
Solution:
  1. Step 1: Understand scalar addition with arrays

    NumPy supports adding a scalar to each element of an array.
  2. Step 2: Check code for errors

    The code adds 5 to each element of arr1 correctly, no error occurs.
  3. Final Answer:

    No error, scalar addition works element-wise -> Option B
  4. Quick Check:

    Scalar + array = element-wise addition [OK]
Quick Trick: Adding scalar to array adds to each element [OK]
Common Mistakes:
  • Thinking scalar addition causes error
  • Believing scalar must be array
  • Confusing syntax errors with runtime

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes