Bird
Raised Fist0
Kotlinprogramming~10 mins

String templates and interpolation in Kotlin - Interactive Code Practice

Choose your learning style10 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.

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

  1. Step 1: Understand Kotlin string templates

    Kotlin uses $variable to insert variable values inside strings.
  2. Step 2: Identify correct syntax for variable insertion

    The syntax $name directly inserts the value of name into the string.
  3. Final Answer:

    println("Hello, $name!") -> Option C
  4. 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

  1. Step 1: Recall expression syntax in string templates

    Kotlin uses ${expression} to evaluate expressions inside strings.
  2. Step 2: Identify correct expression usage

    ${a + b} correctly calculates the sum and inserts it.
  3. Final Answer:

    println("Sum is ${a + b}") -> Option A
  4. 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

  1. Step 1: Evaluate the expression inside the string template

    The expression ${price + tax} adds 10 + 2 = 12.
  2. Step 2: Print the resulting string

    The string becomes "Total: 12" and is printed.
  3. Final Answer:

    Total: 12 -> Option B
  4. 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

  1. Step 1: Understand how $count+1 is interpreted

    Kotlin treats $count as variable and +1 as plain text, so it prints "5+1" literally.
  2. Step 2: Correct way to calculate sum in string

    Use ${count + 1} to evaluate the sum inside the string.
  3. Final Answer:

    It will print 'Count is 5+1' literally, not sum -> Option A
  4. 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

  1. Step 1: Understand escaping dollar sign in Kotlin strings

    To print a literal dollar sign, use \$ to escape it.
  2. Step 2: Combine escaped dollar sign with variable

    "\$$price" prints a literal $ followed by the value of price.
  3. Final Answer:

    println("\$$price") -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard