Bird
Raised Fist0
NumPydata~20 mins

Specifying dtype during creation in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
dtype Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of numpy array with specified dtype
What is the output of this code snippet?
import numpy as np
arr = np.array([1, 2, 3], dtype='float32')
print(arr.dtype)
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='float32')
print(arr.dtype)
Aint64
Bint32
Cfloat64
Dfloat32
Attempts:
2 left
💡 Hint
Check the dtype argument passed during array creation.
❓ data_output
intermediate
2:00remaining
Resulting array values with dtype conversion
What is the output array when specifying dtype as int8 for this input?
import numpy as np
arr = np.array([300, -130, 127], dtype='int8')
print(arr)
NumPy
import numpy as np
arr = np.array([300, -130, 127], dtype='int8')
print(arr)
A[-44, 126, 127]
B[44, 126, 127]
C[44, -126, 127]
D[-44, -126, 127]
Attempts:
2 left
💡 Hint
Remember int8 stores values from -128 to 127 and wraps around on overflow.
🔧 Debug
advanced
1:30remaining
Identify error when specifying dtype incorrectly
What error does this code raise?
import numpy as np
arr = np.array([1, 2, 3], dtype='flot64')
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='flot64')
ATypeError: data type 'flot64' not understood
BValueError: invalid dtype string
CSyntaxError: invalid syntax
DNo error, creates array with default dtype
Attempts:
2 left
💡 Hint
Check if the dtype string is a valid numpy type.
🚀 Application
advanced
2:00remaining
Choosing dtype for memory optimization
You have a large dataset of integers ranging from 0 to 1000. Which dtype should you specify to minimize memory usage without losing data?
Anp.int8
Bnp.int32
Cnp.uint16
Dnp.float64
Attempts:
2 left
💡 Hint
Consider the range of values and the size limits of each dtype.
🧠 Conceptual
expert
2:30remaining
Effect of dtype on arithmetic operations
Given two numpy arrays:
import numpy as np
arr1 = np.array([1000], dtype='int16')
arr2 = np.array([1000], dtype='int16')
result = arr1 + arr2
print(result, result.dtype)

What is the output and dtype of result?
NumPy
import numpy as np
arr1 = np.array([1000], dtype='int16')
arr2 = np.array([1000], dtype='int16')
result = arr1 + arr2
print(result, result.dtype)
A[2000] int16
B[2000] int32
C[-25536] int16
D[-25536] int32
Attempts:
2 left
💡 Hint
int16 max value is 32767; check if addition overflows.

Practice

(1/5)
1. What does specifying dtype when creating a NumPy array do?
easy
A. It sets the type of data stored in the array.
B. It changes the shape of the array.
C. It sorts the array elements.
D. It deletes elements from the array.

Solution

  1. Step 1: Understand the role of dtype in NumPy

    The dtype parameter defines the type of data (like int, float) stored in the array.
  2. Step 2: Compare with other options

    Changing shape, sorting, or deleting elements are unrelated to dtype.
  3. Final Answer:

    It sets the type of data stored in the array. -> Option A
  4. Quick Check:

    dtype controls data type = A [OK]
Hint: dtype controls data type stored in array [OK]
Common Mistakes:
  • Confusing dtype with array shape
  • Thinking dtype sorts or deletes elements
  • Assuming dtype changes array size
2. Which of the following is the correct way to create a NumPy array of integers using dtype?
easy
A. np.array([1, 2, 3], type='int')
B. np.array([1, 2, 3], dtype='int')
C. np.array([1, 2, 3], dtype=int32)
D. np.array([1, 2, 3], dtype='string')

Solution

  1. Step 1: Check correct dtype syntax

    The correct parameter is dtype with a string like 'int' or a NumPy dtype like np.int32.
  2. Step 2: Evaluate each option

    np.array([1, 2, 3], dtype='int') uses dtype='int' which is valid. np.array([1, 2, 3], type='int') uses wrong parameter type. np.array([1, 2, 3], dtype=int32) uses dtype=int32 without quotes or np.int32, which causes error. np.array([1, 2, 3], dtype='string') uses dtype='string' which is valid but not integer.
  3. Final Answer:

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

    Correct dtype syntax = A [OK]
Hint: Use dtype='int' or dtype=np.int32 for integers [OK]
Common Mistakes:
  • Using 'type' instead of 'dtype'
  • Missing quotes around dtype string
  • Using wrong dtype names like 'string' for integers
3. What is the output of this code?
import numpy as np
arr = np.array([1.5, 2.3, 3.7], dtype='int')
print(arr)
medium
A. [1.5 2.3 3.7]
B. Error: cannot convert float to int
C. [2 3 4]
D. [1 2 3]

Solution

  1. Step 1: Understand dtype conversion

    Specifying dtype='int' converts floats to integers by truncating decimals.
  2. Step 2: Apply conversion to each element

    1.5 becomes 1, 2.3 becomes 2, 3.7 becomes 3, so array is [1 2 3].
  3. Final Answer:

    [1 2 3] -> Option D
  4. Quick Check:

    Float to int truncates decimals = D [OK]
Hint: dtype='int' truncates floats to integers [OK]
Common Mistakes:
  • Expecting rounding instead of truncation
  • Thinking dtype conversion causes error
  • Confusing output with original float values
4. Find the error in this code snippet:
import numpy as np
arr = np.array([True, False, 1, 0], dtype='bool')
print(arr)
medium
A. The dtype 'bool' is invalid.
B. Mixing booleans and integers causes error.
C. No error; code runs and prints [ True False True False].
D. Must convert integers to strings before creating array.

Solution

  1. Step 1: Check dtype 'bool' validity

    'bool' is a valid dtype in NumPy for boolean arrays.
  2. Step 2: Understand mixing booleans and integers

    NumPy converts integers 1 and 0 to True and False respectively without error.
  3. Step 3: Confirm output

    The array prints as [ True False True False].
  4. Final Answer:

    No error; code runs and prints [ True False True False]. -> Option C
  5. Quick Check:

    Boolean dtype accepts 0/1 as False/True = B [OK]
Hint: dtype='bool' converts 0/1 to False/True automatically [OK]
Common Mistakes:
  • Thinking mixing bool and int causes error
  • Assuming dtype 'bool' is invalid
  • Believing integers must be strings for bool dtype
5. You have a list of numbers with some as strings: data = ["1", 2, "3", 4]. How do you create a NumPy array of integers from this list?
hard
A. np.array(data, dtype='int')
B. np.array(data, dtype='float')
C. np.array(data, dtype='str')
D. np.array(data, dtype='bool')

Solution

  1. Step 1: Understand mixed data types in list

    The list has strings and integers; NumPy can convert strings to integers if dtype='int' is specified.
  2. Step 2: Check dtype options

    Using dtype='int' converts all elements to integers. dtype='float' converts to floats, dtype='str' keeps strings, dtype='bool' converts to booleans.
  3. Step 3: Choose correct dtype for integers

    To get integers, use dtype='int'.
  4. Final Answer:

    np.array(data, dtype='int') -> Option A
  5. Quick Check:

    Convert mixed strings to int with dtype='int' = C [OK]
Hint: Use dtype='int' to convert strings to integers in array [OK]
Common Mistakes:
  • Using dtype='str' keeps strings, not integers
  • Using dtype='bool' changes data meaning
  • Choosing dtype='float' when integers needed
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