Bird
0
0

What will this Swift code print?

medium📝 Predict Output Q13 of 15
Swift - Basics and Runtime
What will this Swift code print?
let device = "iPhone"
print("I love my \(device)!")
AI love my iPhone!
BError: Invalid syntax
CI love my \(device)!
DI love my device!
Step-by-Step Solution
Solution:
  1. Step 1: Understand string interpolation in Swift

    Using \(variable) inside a string inserts the variable's value.
  2. Step 2: Apply to given code

    The variable device is "iPhone", so the print outputs "I love my iPhone!".
  3. Final Answer:

    I love my iPhone! -> Option A
  4. Quick Check:

    String interpolation = "I love my iPhone!" [OK]
Quick Trick: Remember \(var) inserts value inside strings [OK]
Common Mistakes:
  • Printing the variable name instead of its value
  • Confusing escape characters
  • Thinking it causes a syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes