0
0
Kotlinprogramming~10 mins

String templates and interpolation in Kotlin - Interactive Code Practice

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

Complete the code to print the value of the variable name using string templates.

Kotlin
val name = "Alice"
println("Hello, [1]!")
Drag options to blanks, or click blank then click option'
A$name
B{name}
Cname
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the dollar sign and just writing the variable name.
Using curly braces without the dollar sign.
Putting the variable name in quotes which prints the name literally.
2fill in blank
medium

Complete the code to print the sum of two variables a and b inside a string using string templates.

Kotlin
val a = 5
val b = 3
println("Sum is [1]")
Drag options to blanks, or click blank then click option'
A$a + $b
B$a + b
C${a + b}
Da + b
Attempts:
3 left
💡 Hint
Common Mistakes
Writing $a + $b which prints the variables separately without adding.
Not using curly braces for expressions.
Writing the expression outside the string.
3fill in blank
hard

Fix the error in the code to correctly print the length of the string word using string templates.

Kotlin
val word = "Kotlin"
println("Length: [1]")
Drag options to blanks, or click blank then click option'
A${word.length()}
B$word.length
C$word.length()
D${word.length}
Attempts:
3 left
💡 Hint
Common Mistakes
Using $word.length which prints the word and then '.length' literally.
Using parentheses after length which causes an error.
Not using ${} to evaluate the expression.
4fill in blank
hard

Fill both blanks to create a string that shows the uppercase version of city and its length.

Kotlin
val city = "Paris"
val info = "City: [1], Length: [2]"
println(info)
Drag options to blanks, or click blank then click option'
Acity.uppercase()
Bcity.length
C${city.length}
D${city.uppercase()}
Attempts:
3 left
💡 Hint
Common Mistakes
Not using ${} for expressions.
Using city.uppercase() without ${} which prints the method call literally.
Using city.length without ${} which prints the variable name and '.length' literally.
5fill in blank
hard

Fill all three blanks to create a greeting message that includes the user's name in uppercase, age, and a message if they are adult or not.

Kotlin
val name = "Bob"
val age = 20
val message = "Hello, [1]! You are [2] years old and you are [3]."
println(message)
Drag options to blanks, or click blank then click option'
A${name.uppercase()}
B$age
Can adult
Da minor
Attempts:
3 left
💡 Hint
Common Mistakes
Not using ${} for uppercase expression.
Putting age inside ${} unnecessarily.
Using the wrong adult/minor message.