Bird
0
0

Find the mistake in this code regarding type promotion:

medium📝 Debug Q7 of 15
NumPy - Array Operations
Find the mistake in this code regarding type promotion:
import numpy as np
arr1 = np.array([1, 2, 3], dtype=np.uint8)
arr2 = np.array([1.5, 2.5, 3.5], dtype=np.float64)
result = arr1 * arr2
print(result.dtype)
ANo mistake, dtype is float64
BMultiplication causes integer overflow
CResult dtype is uint8, which is incorrect
DOperation causes a TypeError
Step-by-Step Solution
Solution:
  1. Step 1: Identify input dtypes

    arr1 is uint8, arr2 is float64.
  2. Step 2: Apply type promotion rules

    uint8 and float64 promote to float64 safely.
  3. Final Answer:

    No mistake, dtype is float64 -> Option A
  4. Quick Check:

    uint8 * float64 = float64 [OK]
Quick Trick: Unsigned int with float promotes to float64 [OK]
Common Mistakes:
  • Expecting overflow error
  • Assuming result stays uint8
  • Thinking operation raises TypeError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes