Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In Kotlin, string templates use the dollar sign ($) followed by the variable name to insert its value inside a string.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
To evaluate an expression inside a string template, use ${expression}. Here, ${a + b} calculates the sum and inserts it.
3fill in blank
hardFix 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'
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.
✗ Incorrect
In Kotlin, to get the length property inside a string template, use ${word.length}. The length is a property, not a function, so no parentheses.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Use ${city.uppercase()} to get the uppercase string and ${city.length} to get the length inside the string template.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using ${} for uppercase expression.
Putting age inside ${} unnecessarily.
Using the wrong adult/minor message.
✗ Incorrect
Use ${name.uppercase()} to show the uppercase name, $age to insert the age, and 'an adult' as the message for age 20.