0
0
Swiftprogramming~20 mins

String interpolation in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift String Interpolation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.")
AMy name is Alice and I am 30 years old.
BMy name is \(name) and I am \(age) years old.
CMy name is name and I am age years old.
DMy name is Alice and I am age years old.
Attempts:
2 left
💡 Hint
Look at how variables are inserted inside the string using \(variable).
Predict Output
intermediate
2: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)")
ASum: x + y, Product: x * y
BSum: 8, Product: x * y
CSum: 53, Product: 15
DSum: 8, Product: 15
Attempts:
2 left
💡 Hint
Inside \( ), Swift evaluates the expression before inserting it.
🔧 Debug
advanced
2:00remaining
Identify the error in string interpolation
What error does this Swift code produce?
Swift
let celsius = 22
print("Current temperature is \celsius degrees.")
APrints: Current temperature is temperature degrees.
BPrints: Current temperature is \temperature degrees.
CSyntaxError: Missing parentheses around variable in string interpolation
DRuntimeError: Variable temperature not found
Attempts:
2 left
💡 Hint
Check how variables are inserted inside strings in Swift.
Predict Output
advanced
2: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)
A
Hello, Bob!
Your score is 95.
B
Hello, \(name)!
Your score is \(score).
CHello, Bob! Your score is 95.
D
Hello, name!
Your score is score.
Attempts:
2 left
💡 Hint
Multiline strings keep line breaks and support interpolation the same way.
🧠 Conceptual
expert
2:00remaining
Effect of using backslash without parentheses in interpolation
What happens if you write this Swift code? let count = 10 print("Count is \count")
APrints: Count is \count
BCompiler error due to missing parentheses in string interpolation
CPrints: Count is count
DPrints: Count is 10
Attempts:
2 left
💡 Hint
Swift requires parentheses around variables in string interpolation.