Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Data Types

What will be the output of this Swift code?

let x: Int = 5
let y: Double = 3.0
let result = Double(x) + y
print(result)
A8
B8.0
C8.00
DError: Cannot add Int and Double
Step-by-Step Solution
Solution:
  1. Step 1: Analyze type conversion in addition

    Int x is converted to Double before addition with y (Double).
  2. Step 2: Calculate the sum and print

    5 converted to 5.0, added to 3.0 equals 8.0, printed as Double.
  3. Final Answer:

    8.0 -> Option B
  4. Quick Check:

    Int to Double conversion = 8.0 [OK]
Quick Trick: Convert Int to Double before adding to Double [OK]
Common Mistakes:
  • Expecting integer 8
  • Ignoring type conversion
  • Assuming error on mixed types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes