Bird
0
0

What is the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Data Types
What is the output of this Swift code?
let x: Int = 4
let y: Double = 2.5
let z = Double(x) + y
print(z)
A6
B6.5
CType error at compile time
D4.5
Step-by-Step Solution
Solution:
  1. Step 1: Convert Int to Double explicitly

    Double(x) converts 4 to 4.0 so it can be added to y (2.5).
  2. Step 2: Add the two Double values

    4.0 + 2.5 equals 6.5.
  3. Final Answer:

    6.5 -> Option B
  4. Quick Check:

    Explicit conversion allows addition = 6.5 [OK]
Quick Trick: Convert Int to Double before adding to Double [OK]
Common Mistakes:
  • Expecting implicit conversion
  • Getting a compile error due to missing conversion
  • Adding Int and Double directly without conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes