Bird
0
0

Consider this code snippet:

medium📝 Debug Q14 of 15
NumPy - Array Operations
Consider this code snippet:
import numpy as np
arr = np.array([1, 2, 3], dtype=np.int8)
result = arr + 1.5

Why might the result's dtype not be what you expect if you change arr to np.int16?
ABecause int16 promotes to string, causing unexpected dtype.
BBecause int16 does not promote and causes an error.
CBecause int16 promotes to float64, changing result dtype.
DBecause int16 promotes to int8, lowering precision.
Step-by-Step Solution
Solution:
  1. Step 1: Understand type promotion with int8 and int16

    Adding float 1.5 to int8 promotes to float64. Similarly, int16 also promotes to float64.
  2. Step 2: Explain why dtype changes with int16

    int16 has higher precision than int8 but still promotes to float64 when added to float, so result dtype is float64, not int16.
  3. Final Answer:

    Because int16 promotes to float64, changing result dtype. -> Option C
  4. Quick Check:

    int16 + float = float64 [OK]
Quick Trick: Adding float promotes int16 to float64, not int16 [OK]
Common Mistakes:
  • Thinking int16 causes error with float addition
  • Assuming int16 promotes down to int8
  • Believing result becomes string dtype

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes