Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
NumPy - Array Data Types
What will be the output of the following code?
import numpy as np
arr = np.array([1.7, 2.3, 3.9])
new_arr = arr.astype(int)
print(new_arr)
A[2 3 4]
B[1.7 2.3 3.9]
C[1 2 3]
DError: Cannot convert float to int
Step-by-Step Solution
Solution:
  1. Step 1: Understand astype(int) on float array

    Converting floats to int using astype(int) truncates the decimal part (does not round).
  2. Step 2: Apply truncation to each element

    1.7 becomes 1, 2.3 becomes 2, 3.9 becomes 3.
  3. Final Answer:

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

    astype(int) truncates floats to integers [OK]
Quick Trick: astype(int) truncates decimals, does not round [OK]
Common Mistakes:
  • Assuming astype(int) rounds instead of truncates
  • Expecting original array to change
  • Thinking conversion causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes