Bird
0
0

What will this Swift code print?

medium📝 Predict Output Q5 of 15
Swift - Variables and Constants
What will this Swift code print?
let count = 3
print("You have \(count + 2) new messages.")
AYou have 5 new messages.
BYou have count + 2 new messages.
CYou have \(count + 2) new messages.
DYou have 3 + 2 new messages.
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate expression inside interpolation

    The expression count + 2 equals 3 + 2 = 5.
  2. Step 2: Insert evaluated result into string

    The string will show "You have 5 new messages."
  3. Final Answer:

    You have 5 new messages. -> Option A
  4. Quick Check:

    Expressions inside \( ) are evaluated before insertion [OK]
Quick Trick: You can do math inside \( ) in strings [OK]
Common Mistakes:
  • Thinking interpolation only inserts variables, not expressions
  • Printing the expression literally
  • Confusing variable name with its value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes