0
0
NumPydata~20 mins

Integer types (int8, int16, int32, int64) in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Integer Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy int8 overflow behavior
What is the output of this code snippet using numpy int8 type?
NumPy
import numpy as np
x = np.array([127], dtype=np.int8)
y = x + 1
print(y[0])
A-128
B128
COverflowError
D127
Attempts:
2 left
💡 Hint
Remember that int8 can only hold values from -128 to 127 and wraps around on overflow.
data_output
intermediate
2:00remaining
Size in bytes of different numpy integer types
What is the output of this code showing the itemsize of numpy integer types?
NumPy
import numpy as np
sizes = {t: np.dtype(t).itemsize for t in ['int8', 'int16', 'int32', 'int64']}
print(sizes)
A{'int8': 1, 'int16': 2, 'int32': 4, 'int64': 8}
B{'int8': 8, 'int16': 16, 'int32': 32, 'int64': 64}
C{'int8': 0, 'int16': 1, 'int32': 2, 'int64': 4}
D{'int8': 2, 'int16': 4, 'int32': 8, 'int64': 16}
Attempts:
2 left
💡 Hint
Itemsize is the number of bytes used to store each integer type.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy int16 overflow addition
What error will this code raise when adding two numpy int16 arrays?
NumPy
import numpy as np
x = np.array([32767], dtype=np.int16)
y = np.array([1], dtype=np.int16)
z = x + y
print(z[0])
ATypeError
BOverflowError
CValueError
DNo error, output is -32768
Attempts:
2 left
💡 Hint
Check how numpy handles overflow for fixed-size integer types.
🧠 Conceptual
advanced
2:00remaining
Range of values for numpy int32 type
What is the correct range of values that a numpy int32 type can represent?
A-128 to 127
B-2,147,483,648 to 2,147,483,647
C-32,768 to 32,767
D0 to 4,294,967,295
Attempts:
2 left
💡 Hint
int32 is a signed 32-bit integer.
🚀 Application
expert
2:00remaining
Choosing the smallest numpy integer type for given data
You have a dataset with integer values ranging from -100 to 100. Which numpy integer type is the most memory efficient to store this data without overflow?
Anp.uint8
Bnp.int32
Cnp.int8
Dnp.int16
Attempts:
2 left
💡 Hint
Check the range of int8 and uint8 types.