Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
NumPy - Array Data Types
What will be the output of this code?
import numpy as np
arr = np.array([300], dtype='int8')
print(arr[0])
AError
B300
C-44
D44
Step-by-Step Solution
Solution:
  1. Step 1: Understand int8 range and overflow

    int8 can store -128 to 127. 300 is outside this range, so it wraps around using modulo 256.
  2. Step 2: Calculate wrapped value

    300 mod 256 = 44, but since int8 is signed, values above 127 wrap to negative numbers. 44 is within positive range, but 300 actually wraps to -44 because 300 - 256 = 44, and 44 corresponds to -44 in signed int8 representation.
  3. Final Answer:

    -44 -> Option C
  4. Quick Check:

    int8 overflow wraps modulo 256 with signed interpretation [OK]
Quick Trick: int8 stores values modulo 256, causing overflow wrap [OK]
Common Mistakes:
  • Expecting 300 as output
  • Assuming error on overflow

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes