Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
NumPy - Array Data Types
Identify the error in the following code snippet:
import numpy as np
arr = np.array([1, 2, 3])
new_arr = arr.astype('int32')
print(new_arr.astype('float64'))
AError because 'int32' is not a valid dtype string
BNo error; code runs and prints float64 array
CError because astype() cannot be chained
DError because original array is modified
Step-by-Step Solution
Solution:
  1. Step 1: Check dtype string validity

    'int32' and 'float64' are valid NumPy dtype strings for 32-bit int and 64-bit float.
  2. Step 2: Verify chaining of astype()

    Chaining astype() calls is allowed because each returns a new array.
  3. Final Answer:

    No error; code runs and prints float64 array -> Option B
  4. Quick Check:

    Valid dtype strings and chaining astype() work [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes