0
0
Kotlinprogramming~10 mins

String concatenation vs templates in Kotlin - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to concatenate two strings using the + operator.

Kotlin
val greeting = "Hello, " [1] "World!"
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using arithmetic operators like - or * instead of + for strings.
2fill in blank
medium

Complete the code to use a string template to insert the variable name into the greeting.

Kotlin
val name = "Alice"
val greeting = "Hello, [1]!"
Drag options to blanks, or click blank then click option'
A{name}
Bname
C"name"
D$name
Attempts:
3 left
💡 Hint
Common Mistakes
Writing the variable name without $ inside the string.
Using curly braces without $.
3fill in blank
hard

Fix the error in the string template to correctly include the variable age.

Kotlin
val age = 30
val message = "I am [1] years old."
Drag options to blanks, or click blank then click option'
A${age}
Bage
C$age
D"age"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $variable without braces when followed by text.
Writing variable name without $.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, using string templates and a condition.

Kotlin
val words = listOf("apple", "bat", "carrot")
val lengths = {word: [1] for word in words if word.length [2] 3}
Drag options to blanks, or click blank then click option'
Aword.length
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.length for the value.
Using wrong comparison operators like < or ==.
5fill in blank
hard

Fill all three blanks to create a message using string templates with expressions.

Kotlin
val x = 5
val y = 10
val message = "Sum of [1] and [2] is [3]"
Drag options to blanks, or click blank then click option'
Ax
By
C${x + y}
D$x
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $ before variables inside strings.
Not using ${} for expressions.