Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q13 of 15
Swift - Variables and Constants
What will be the output of this Swift code?
let pi: Double = 3
print(pi)
AError: Cannot assign Int to Double
B3
C3.00
D3.0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the type annotation and assigned value

    The variable pi is declared as a Double but assigned the integer 3. Swift converts the integer 3 to 3.0 automatically.
  2. Step 2: Understand print output for Double

    Printing a Double with value 3 prints as 3.0 by default.
  3. Final Answer:

    3.0 -> Option D
  4. Quick Check:

    Double 3 prints as 3.0 [OK]
Quick Trick: Assigning Int to Double converts to decimal automatically [OK]
Common Mistakes:
  • Expecting a compile error
  • Thinking print shows integer 3
  • Confusing Double formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes