Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Fundamentals
What will be the output of this code?
import numpy as np
list1 = [1, 2, 3]
arr1 = np.array(list1)
print(arr1 + [4, 5, 6])
AError: cannot add list to array
B[5 7 9]
C[1 2 3 4 5 6]
D[4 5 6]
Step-by-Step Solution
Solution:
  1. Step 1: Create NumPy array from list

    arr1 is [1, 2, 3].
  2. Step 2: Add list element-wise to array

    NumPy converts list to array and adds element-wise: [1+4, 2+5, 3+6] = [5, 7, 9].
  3. Final Answer:

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

    Array + list = element-wise addition [OK]
Quick Trick: NumPy adds arrays and lists element-wise if sizes match [OK]
Common Mistakes:
  • Expecting concatenation instead of addition
  • Thinking it raises an error
  • Confusing addition with multiplication

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes