A string template in Kotlin is a way to include variables or expressions inside a string by using the $ symbol before the variable name or ${} for expressions.
Click to reveal answer
beginner
How do you include a variable name inside a string using Kotlin string templates?
You write the string like this: "Hello, $name!". Kotlin replaces $name with the value of the variable name.
Click to reveal answer
beginner
How do you include an expression like 2 + 3 inside a Kotlin string template?
You use curly braces around the expression: "The sum is ${2 + 3}". Kotlin calculates the expression and inserts the result.
Click to reveal answer
intermediate
What happens if you want to include a literal dollar sign $ in a Kotlin string template?
You escape it by using \$. For example, "Price is \$5" will show Price is $5.
Click to reveal answer
intermediate
Can you use string templates with multiline strings in Kotlin?
Yes! You can use string templates inside triple-quoted strings """...""". Variables and expressions inside $ or ${} will be replaced as usual.
Click to reveal answer
How do you insert the value of a variable age into a Kotlin string?
A"I am $age years old"
B"I am age years old"
C"I am ${age}"
D"I am ${age years old}"
✗ Incorrect
Use $age or ${age} to insert the variable value. Option A is correct and simplest.
What is the output of: val x = 5; println("x + 2 = ${x + 2}")?
Ax + 2 = 5 + 2
Bx + 2 = x + 2
Cx + 2 = ${x + 2}
Dx + 2 = 7
✗ Incorrect
The expression inside ${} is evaluated, so it prints 7.
How do you write a literal dollar sign in a Kotlin string template?
A$$
B\$
C$
D\\$
✗ Incorrect
Use a backslash before the dollar sign to escape it: \$.
Which of these is a valid Kotlin string template expression?
A"Hello, ${user.name!}"
B"Hello, $user.name!"
C"Hello, ${user.name}!"
D"Hello, $user.name!" with quotes
✗ Incorrect
You must use ${} for expressions like accessing a property.
Can you use string templates inside triple-quoted strings in Kotlin?
AYes, variables and expressions are replaced.
BNo, string templates don't work in triple quotes.
COnly variables work, not expressions.
DOnly expressions work, not variables.
✗ Incorrect
Triple-quoted strings support string templates fully.
Explain how to use string templates in Kotlin to include variables and expressions inside strings.
Think about how you write a sentence with a variable inside.
You got /4 concepts.
Describe how to include a literal dollar sign character in a Kotlin string that uses string templates.
Remember that $ is special in string templates.
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to include a variable name inside a Kotlin string using string templates?
easy
A. println("Hello, +name+")
B. println("Hello, {name}!")
C. println("Hello, $name!")
D. println("Hello, %name%!")
Solution
Step 1: Understand Kotlin string templates
Kotlin uses $variable to insert variable values inside strings.
Step 2: Identify correct syntax for variable insertion
The syntax $name directly inserts the value of name into the string.
Final Answer:
println("Hello, $name!") -> Option C
Quick Check:
Use $variable for string templates [OK]
Hint: Use $variable to insert variables inside strings [OK]
Common Mistakes:
Using curly braces without $ like {name}
Concatenating with + inside strings
Using % signs instead of $
2. Which of the following is the correct syntax to include an expression inside a Kotlin string template?
easy
A. println("Sum is ${a + b}")
B. println("Sum is $a + $b")
C. println("Sum is $[a + b]")
D. println("Sum is $(a + b)")
Solution
Step 1: Recall expression syntax in string templates
Kotlin uses ${expression} to evaluate expressions inside strings.
Step 2: Identify correct expression usage
${a + b} correctly calculates the sum and inserts it.
Final Answer:
println("Sum is ${a + b}") -> Option A
Quick Check:
Use ${expression} for calculations inside strings [OK]
Hint: Use ${} to insert expressions inside strings [OK]
Common Mistakes:
Using $a + $b prints variables separately, not sum
Using square brackets [] instead of curly braces
Using parentheses () without $
3. What will be the output of the following Kotlin code?
val price = 10
val tax = 2
println("Total: ${price + tax}")
medium
A. Total: 10 + 2
B. Total: 12
C. Total: ${price + tax}
D. Total: price + tax
Solution
Step 1: Evaluate the expression inside the string template
The expression ${price + tax} adds 10 + 2 = 12.
Step 2: Print the resulting string
The string becomes "Total: 12" and is printed.
Final Answer:
Total: 12 -> Option B
Quick Check:
Expression inside ${} is evaluated [OK]
Hint: Expressions inside ${} are calculated before printing [OK]
Common Mistakes:
Printing the expression literally without evaluation
Confusing $variable with ${expression}
Expecting concatenation without braces
4. Identify the error in this Kotlin code snippet:
val count = 5
println("Count is $count+1")
medium
A. It will print 'Count is 5+1' literally, not sum
B. Runtime error because of invalid string
C. Syntax error due to missing braces
D. It will print 'Count is 6' correctly
Solution
Step 1: Understand how $count+1 is interpreted
Kotlin treats $count as variable and +1 as plain text, so it prints "5+1" literally.
Step 2: Correct way to calculate sum in string
Use ${count + 1} to evaluate the sum inside the string.
Final Answer:
It will print 'Count is 5+1' literally, not sum -> Option A
Quick Check:
Use ${} for expressions, else prints text [OK]
Hint: Use ${} to calculate expressions, else prints text [OK]
Common Mistakes:
Assuming $count+1 calculates sum automatically
Missing braces for expressions
Confusing string concatenation with templates
5. You want to print the price with a dollar sign, for example: $20. Which Kotlin string correctly shows the dollar sign before the number stored in price?
hard
A. println("$price")
B. println("\$price")
C. println("$\$price")
D. println("\$$price")
Solution
Step 1: Understand escaping dollar sign in Kotlin strings
To print a literal dollar sign, use \$ to escape it.
Step 2: Combine escaped dollar sign with variable
"\$$price" prints a literal $ followed by the value of price.
Final Answer:
println("\$$price") -> Option D
Quick Check:
Escape $ with \$ to print it literally [OK]
Hint: Escape $ as \$ to print dollar sign literally [OK]
Common Mistakes:
Not escaping $ prints variable name or causes error
Using $\$ instead of \$$
Confusing escape sequences
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.