0
0
Kotlinprogramming~20 mins

String concatenation vs templates in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Mastery in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AHello, name. You are age years old.
BHello, Anna. You are 30 years old.
CHello, Anna. You are age years old.
DHello, name. You are 30 years old.
Attempts:
2 left
💡 Hint
Look at how variables are combined with strings using + operator.
Predict Output
intermediate
2: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}")
ASum of 5 and 10 is 15
BSum of x and y is x + y
CSum of $x and $y is ${x + y}
DSum of 5 and 10 is x + y
Attempts:
2 left
💡 Hint
Remember that expressions inside ${} are evaluated.
🔧 Debug
advanced
2:00remaining
Identify the error in string concatenation
What error does this Kotlin code produce?
Kotlin
val name = "Bob"
val message = "Hello, " + name + ". " + "!"
println(message)
ASyntaxError: Unexpected token '.'
BTypeError: Cannot concatenate String and Char
CRuntimeException: Null pointer
DNo error, prints: Hello, Bob.!
Attempts:
2 left
💡 Hint
Check the placement of the dot operator in concatenation.
Predict Output
advanced
2: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)
AResult: 3 + 4 = 7
BResult: $a + $b = a + b
CResult: $a + $b = 7
DResult: 3 + 4 = a + b
Attempts:
2 left
💡 Hint
Notice the quotes around $a and $b inside concatenation.
🧠 Conceptual
expert
2:00remaining
Why prefer string templates over concatenation?
Which is the best reason to prefer Kotlin string templates over string concatenation?
ATemplates allow using variables without declaring them first.
BTemplates run faster because they use less memory than concatenation.
CConcatenation is deprecated and will be removed in future Kotlin versions.
DTemplates improve readability and reduce errors by embedding variables directly.
Attempts:
2 left
💡 Hint
Think about code clarity and ease of writing.