Step 1: Calculate number of elements and size per element
The array has 4 elements, each of type int32 which uses 4 bytes.
Step 2: Multiply elements by bytes per element
Total bytes = 4 elements * 4 bytes = 16 bytes.
Final Answer:
16 -> Option C
Quick Check:
4 elements x 4 bytes = 16 bytes [OK]
Hint: Multiply element count by itemsize for nbytes [OK]
Common Mistakes:
Assuming int32 uses 8 bytes
Confusing nbytes with number of elements
Forgetting to multiply by element size
4. You run this code but get an error:
import numpy as np
arr = np.array([1, 2, 3])
print(arr.nbytes())
What is the cause of the error?
medium
A. nbytes is an attribute, not a method; remove parentheses
B. Array must be created with dtype specified
C. nbytes only works on 2D arrays
D. You must import nbytes separately
Solution
Step 1: Identify the error cause
The code calls arr.nbytes() with parentheses, but nbytes is an attribute, not a function.
Step 2: Correct usage
Access nbytes without parentheses: arr.nbytes.
Final Answer:
nbytes is an attribute, not a method; remove parentheses -> Option A
Quick Check:
Attributes have no parentheses [OK]
Hint: Remember: attributes like nbytes have no parentheses [OK]
Common Mistakes:
Adding parentheses to attributes
Thinking dtype is required for nbytes
Believing nbytes only works on certain array shapes
5. You have a large NumPy array big_arr with 1 million elements of type float64. You want to reduce memory usage without losing much precision. Which approach helps monitor and reduce memory usage effectively?
hard
A. Check big_arr.nbytes and convert to float32 if memory is high
B. Use big_arr.size to reduce elements by half
C. Delete big_arr and recreate with zeros
D. Convert big_arr to Python list to save memory
Solution
Step 1: Monitor memory usage with nbytes
Use big_arr.nbytes to see current memory consumption in bytes.
Step 2: Reduce memory by changing data type
Converting from float64 (8 bytes) to float32 (4 bytes) halves memory usage with minimal precision loss.
Final Answer:
Check big_arr.nbytes and convert to float32 if memory is high -> Option A
Quick Check:
Use nbytes + dtype change to reduce memory [OK]
Hint: Check nbytes then downcast dtype to save memory [OK]
Common Mistakes:
Using size instead of nbytes to measure memory
Converting to Python list increases memory
Deleting array without monitoring memory
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.