Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Data Types
What will be printed by this Swift code?
let a: Double = 3.7
let b: Int = Int(a)
print(b)
A3.7
B4
C3
DCompile error
Step-by-Step Solution
Solution:
  1. Step 1: Convert Double to Int explicitly

    Int(a) truncates the decimal part, converting 3.7 to 3.
  2. Step 2: Print the integer value

    Printing b outputs 3.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Double to Int conversion truncates decimals = 3 [OK]
Quick Trick: Converting Double to Int drops decimal part [OK]
Common Mistakes:
  • Expecting rounding instead of truncation
  • Assuming implicit conversion without error
  • Confusing output with original Double value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes