Bird
0
0

What is the type of variable result and its value after this Swift code runs?

medium📝 Predict Output Q4 of 15
Swift - Variables and Constants
What is the type of variable result and its value after this Swift code runs?
let result = 3 + 4.5
ADouble, 7.5
BInt, 7
CFloat, 7.5
DString, "7.5"
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the expression types

    3 is Int, 4.5 is Double; Swift promotes Int to Double for addition.
  2. Step 2: Determine result type and value

    Result is Double with value 7.5 after addition.
  3. Final Answer:

    Double, 7.5 -> Option A
  4. Quick Check:

    Mixed numeric types result in Double [OK]
Quick Trick: Mixed Int and Double results in Double type [OK]
Common Mistakes:
  • Assuming result is Int and truncates decimal
  • Confusing Float with Double
  • Thinking result is a String

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes