A. Error because 'int32' is not a valid dtype string
B. No error; code runs and prints float64 array
C. Error because astype() cannot be chained
D. Error because original array is modified
Solution
Step 1: Check dtype string validity
'int32' and 'float64' are valid NumPy dtype strings for 32-bit int and 64-bit float.
Step 2: Verify chaining of astype()
Chaining astype() calls is allowed because each returns a new array.
Final Answer:
No error; code runs and prints float64 array -> Option B
Quick Check:
Valid dtype strings and chaining astype() work [OK]
Hint: astype() returns new array; chaining is allowed [OK]
Common Mistakes:
Thinking dtype strings like 'int32' are invalid
Believing astype() modifies original array
Assuming astype() cannot be chained
5. You have a NumPy array arr = np.array([10, 20, 30]) of integers. You want to save memory by converting it to the smallest unsigned integer type that can hold these values. Which astype() call is best?
hard
A. arr.astype('uint8')
B. arr.astype('int8')
C. arr.astype('float16')
D. arr.astype('uint16')
Solution
Step 1: Check the range of values in arr
Values are 10, 20, 30, all positive and less than 255.
Step 2: Choose smallest unsigned integer type
uint8 holds values 0 to 255, so it fits all values and saves memory.
Step 3: Compare with other options
int8 supports -128 to 127 but is signed; float16 uses more memory; uint16 is larger than needed.
Final Answer:
arr.astype('uint8') -> Option A
Quick Check:
Use smallest unsigned int type fitting data [OK]
Hint: Pick smallest unsigned int type covering max value [OK]
Common Mistakes:
Choosing signed int type that wastes range or causes errors
Using float type which uses more memory
Ignoring value ranges when selecting dtype
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.