Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which numpy integer type uses the least amount of memory?
easy
A. int64
B. int16
C. int8
D. int32

Solution

  1. Step 1: Understand integer type sizes

    Each numpy integer type uses a different number of bits: int8 uses 8 bits, int16 uses 16 bits, int32 uses 32 bits, and int64 uses 64 bits.
  2. Step 2: Identify the smallest size

    Since 8 bits is the smallest size, int8 uses the least memory.
  3. Final Answer:

    int8 -> Option C
  4. Quick Check:

    Smallest bits = int8 [OK]
Hint: Smaller int types use fewer bits and less memory [OK]
Common Mistakes:
  • Confusing int8 with int16 or larger types
  • Thinking int64 uses less memory
  • Assuming int32 is smallest
2. Which of the following is the correct way to create a numpy array of type int16?
easy
A. np.array([1, 2, 3], dtype='int16')
B. np.array([1, 2, 3], type='int16')
C. np.array([1, 2, 3], dtype=int8)
D. np.array([1, 2, 3], type=int16)

Solution

  1. Step 1: Recall numpy array creation syntax

    To specify the data type, use the parameter dtype with a string or numpy type.
  2. Step 2: Check each option

    np.array([1, 2, 3], dtype='int16') uses dtype='int16', which is correct. Options B and D use type which is invalid. np.array([1, 2, 3], dtype=int8) uses dtype=int8, which is valid syntax but wrong type.
  3. Final Answer:

    np.array([1, 2, 3], dtype='int16') -> Option A
  4. Quick Check:

    Use dtype='int16' to set type [OK]
Hint: Use dtype='intXX' to set integer type in numpy array [OK]
Common Mistakes:
  • Using 'type' instead of 'dtype'
  • Passing dtype without quotes for string type
  • Confusing int8 and int16 types
3. What will be the output of this code?
import numpy as np
arr = np.array([127, 128], dtype='int8')
print(arr)
medium
A. [127 128]
B. [127 -128]
C. [127 0]
D. Error: value out of range

Solution

  1. Step 1: Understand int8 range

    int8 can store values from -128 to 127. The value 128 is outside this range.
  2. Step 2: Observe overflow behavior

    When 128 is stored in int8, it wraps around to -128 due to overflow.
  3. Final Answer:

    [127 -128] -> Option B
  4. Quick Check:

    int8 overflow wraps 128 to -128 [OK]
Hint: int8 max is 127; values above wrap to negative [OK]
Common Mistakes:
  • Expecting 128 to store correctly in int8
  • Assuming error raised on overflow
  • Thinking 128 becomes 0
4. Identify the error in this code snippet:
import numpy as np
arr = np.array([1000, 2000], dtype='int8')
print(arr)
medium
A. No error, prints [1000 2000]
B. SyntaxError due to dtype
C. ValueError: integer out of range
D. Values overflow and wrap around silently

Solution

  1. Step 1: Check dtype and values

    The array uses int8 which can only hold -128 to 127, but values 1000 and 2000 are much larger.
  2. Step 2: Understand numpy overflow behavior

    Instead of error, numpy silently wraps values using modulo 256, causing overflow.
  3. Final Answer:

    Values overflow and wrap around silently -> Option D
  4. Quick Check:

    int8 overflow wraps large values silently [OK]
Hint: Large values in small int types wrap silently, no error [OK]
Common Mistakes:
  • Expecting ValueError on overflow
  • Thinking values print as is
  • Confusing syntax error with dtype
5. You have a large dataset of whole numbers ranging from -30000 to 30000. Which numpy integer type should you choose to store this data efficiently without overflow?
hard
A. int16
B. int32
C. int8
D. int64

Solution

  1. Step 1: Identify the value range needed

    The data ranges from -30000 to 30000, so the integer type must cover at least this range.
  2. Step 2: Check integer type ranges

    int8 covers -128 to 127 (too small), int16 covers -32768 to 32767 (fits range), int32 and int64 cover larger ranges but use more memory.
  3. Step 3: Choose the smallest type that fits

    int16 fits the range and uses less memory than int32 or int64.
  4. Final Answer:

    int16 -> Option A
  5. Quick Check:

    int16 range covers ±30000 efficiently [OK]
Hint: Pick smallest int type covering data range to save memory [OK]
Common Mistakes:
  • Choosing int8 which is too small
  • Picking int64 unnecessarily
  • Ignoring negative range limits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard