Challenge - 5 Problems
String Mastery in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of string concatenation vs template
What is the output of this Kotlin code snippet?
Kotlin
val name = "Anna" val age = 30 val greeting = "Hello, " + name + ". You are " + age + " years old." println(greeting)
Attempts:
2 left
💡 Hint
Look at how variables are combined with strings using + operator.
✗ Incorrect
The + operator concatenates strings and variables converted to strings. Variables are replaced by their values.
❓ Predict Output
intermediate2:00remaining
Output of string template with expression
What will this Kotlin code print?
Kotlin
val x = 5 val y = 10 println("Sum of $x and $y is ${x + y}")
Attempts:
2 left
💡 Hint
Remember that expressions inside ${} are evaluated.
✗ Incorrect
The $x and $y insert variable values. The expression ${x + y} calculates the sum 15.
🔧 Debug
advanced2:00remaining
Identify the error in string concatenation
What error does this Kotlin code produce?
Kotlin
val name = "Bob" val message = "Hello, " + name + ". " + "!" println(message)
Attempts:
2 left
💡 Hint
Check the placement of the dot operator in concatenation.
✗ Incorrect
The dot before the string literal is invalid syntax in Kotlin concatenation.
❓ Predict Output
advanced2:00remaining
Output of mixed string concatenation and template
What is the output of this Kotlin code?
Kotlin
val a = 3 val b = 4 val result = "Result: " + "$a + $b = " + (a + b) println(result)
Attempts:
2 left
💡 Hint
Notice the quotes around $a and $b inside concatenation.
✗ Incorrect
The string "$a + $b = " is literal text with dollar signs, not variables. Only (a + b) is evaluated.
🧠 Conceptual
expert2:00remaining
Why prefer string templates over concatenation?
Which is the best reason to prefer Kotlin string templates over string concatenation?
Attempts:
2 left
💡 Hint
Think about code clarity and ease of writing.
✗ Incorrect
String templates make code easier to read and write by embedding variables and expressions directly inside strings.