Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
NumPy - Array Data Types
What will be the output of this code?
import numpy as np
arr = np.array([1.9, 2.5, 3.1])
print(arr.astype(int))
A[1 2 3]
B[2 3 4]
C[1 3 3]
D[1.9 2.5 3.1]
Step-by-Step Solution
Solution:
  1. Step 1: Understand float to int conversion

    When converting floats to integers using astype(int), NumPy truncates the decimal part (does not round).
  2. Step 2: Apply truncation to each element

    1.9 becomes 1, 2.5 becomes 2, 3.1 becomes 3, so the output array is [1 2 3].
  3. Final Answer:

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

    astype(int) truncates decimals [OK]
Quick Trick: astype(int) truncates decimals, no rounding [OK]
Common Mistakes:
  • Assuming astype(int) rounds values
  • Expecting float output after conversion
  • Confusing truncation with rounding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes