Challenge - 5 Problems
Swift String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple string interpolation
What is the output of this Swift code?
Swift
let name = "Alice" let age = 30 print("My name is \(name) and I am \(age) years old.")
Attempts:
2 left
💡 Hint
Look at how variables are inserted inside the string using \(variable).
✗ Incorrect
Swift replaces \(variable) with the value of the variable inside the string.
❓ Predict Output
intermediate2:00remaining
String interpolation with expressions
What will this Swift code print?
Swift
let x = 5 let y = 3 print("Sum: \(x + y), Product: \(x * y)")
Attempts:
2 left
💡 Hint
Inside \( ), Swift evaluates the expression before inserting it.
✗ Incorrect
The expressions x + y and x * y are calculated and their results inserted.
🔧 Debug
advanced2:00remaining
Identify the error in string interpolation
What error does this Swift code produce?
Swift
let celsius = 22 print("Current temperature is \celsius degrees.")
Attempts:
2 left
💡 Hint
Check how variables are inserted inside strings in Swift.
✗ Incorrect
Swift requires variables inside string interpolation to be inside parentheses: \(variable).
❓ Predict Output
advanced2:00remaining
String interpolation with multiline strings
What is the output of this Swift code?
Swift
let name = "Bob" let score = 95 let message = """Hello, \(name)!\nYour score is \(score).\n""" print(message)
Attempts:
2 left
💡 Hint
Multiline strings keep line breaks and support interpolation the same way.
✗ Incorrect
The multiline string inserts variable values and preserves line breaks.
🧠 Conceptual
expert2:00remaining
Effect of using backslash without parentheses in interpolation
What happens if you write this Swift code?
let count = 10
print("Count is \count")
Attempts:
2 left
💡 Hint
Swift requires parentheses around variables in string interpolation.
✗ Incorrect
Without parentheses, the backslash is not recognized as interpolation and causes a syntax error.