Challenge - 5 Problems
String Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of simple string template
What is the output of this Kotlin code using string templates?
Kotlin
val name = "Anna" val age = 25 println("My name is $name and I am $age years old.")
Attempts:
2 left
💡 Hint
Remember that $variable inside a string replaces with the variable's value.
✗ Incorrect
In Kotlin, $name and $age inside a string are replaced by their values, so the output shows the actual values.
❓ Predict Output
intermediate2:00remaining
Using expressions inside string templates
What will this Kotlin code print?
Kotlin
val x = 5 val y = 3 println("Sum of $x and $y is ${x + y}")
Attempts:
2 left
💡 Hint
Expressions inside ${} are evaluated before printing.
✗ Incorrect
The expression ${x + y} calculates 5 + 3 and inserts 8 into the string.
🔧 Debug
advanced2:00remaining
Identify the error in string interpolation
What error does this Kotlin code produce?
Kotlin
val count = 10 println("You have $count items in your cart") println("Next item price is $price")
Attempts:
2 left
💡 Hint
Check if all variables used inside strings are declared.
✗ Incorrect
Variable 'price' is not declared anywhere, so Kotlin reports 'Unresolved reference: price'.
❓ Predict Output
advanced2:00remaining
Complex string interpolation with multiline string
What is the output of this Kotlin code?
Kotlin
val user = "Bob" val score = 42 val message = """ Hello, $user! Your score is ${score * 2}. Good luck! """ println(message.trim())
Attempts:
2 left
💡 Hint
Multiline strings keep line breaks and expressions inside ${} are evaluated.
✗ Incorrect
The multiline string preserves line breaks and evaluates ${score * 2} as 84.
🧠 Conceptual
expert2:00remaining
Understanding string template evaluation order
Consider this Kotlin code snippet:
val a = 3
val b = 4
println("Result: ${a + b}")
println("Result: $a + $b")
What will be the output?
Attempts:
2 left
💡 Hint
Expressions inside ${} are evaluated, but $variable just inserts the variable's value.
✗ Incorrect
The first println evaluates ${a + b} to 7. The second prints the values of a and b separately with a plus sign in between.