📖 Scenario: You are creating a simple program to greet users by their names and ages. You want to see two ways to build the greeting message: one by joining strings manually and another by using Kotlin's string templates.
🎯 Goal: Build a Kotlin program that creates a greeting message using string concatenation and then using string templates. Finally, print both messages to compare.
📋 What You'll Learn
Create variables for name and age with exact values
Create a greeting message using string concatenation
Create a greeting message using string templates
Print both greeting messages
💡 Why This Matters
🌍 Real World
Building user-friendly messages in apps, like greetings, notifications, or reports, often requires combining text with variable data.
💼 Career
Knowing how to create readable and maintainable strings is essential for software developers, especially when working on user interfaces or generating dynamic content.
Progress0 / 4 steps
1
Create variables for name and age
Create a String variable called name with the value "Alice" and an Int variable called age with the value 30.
Kotlin
Hint
Use val to create variables and assign the exact values.
2
Create greeting message using string concatenation
Create a String variable called greetingConcat that uses string concatenation with name and age to form the message: Hello, my name is Alice and I am 30 years old.
Kotlin
Hint
Use the + operator to join strings and variables.
3
Create greeting message using string templates
Create a String variable called greetingTemplate that uses Kotlin string templates with name and age to form the same message: Hello, my name is Alice and I am 30 years old.
Kotlin
Hint
Use $variableName inside the string to insert variable values.
4
Print both greeting messages
Print the variables greetingConcat and greetingTemplate each on its own line using println.
Kotlin
Hint
Use println(greetingConcat) and println(greetingTemplate) to show the messages.
Practice
(1/5)
1. What is the main difference between string concatenation and string templates in Kotlin?
easy
A. Templates require explicit conversion of variables to strings.
B. Concatenation uses + to join strings; templates insert variables with $ inside strings.
C. Concatenation is faster than templates in all cases.
D. Concatenation can only join strings, templates can join numbers only.
Solution
Step 1: Understand string concatenation
Concatenation joins strings using the + operator and requires converting non-string variables to strings explicitly.
Step 2: Understand string templates
Templates allow inserting variables directly inside strings using the $ symbol, making code cleaner and easier.
Final Answer:
Concatenation uses + to join strings; templates insert variables with $ inside strings. -> Option B
Quick Check:
Concatenation vs templates = C [OK]
Hint: Templates use $ for variables; concatenation uses + [OK]
Common Mistakes:
Thinking templates need explicit conversion
Believing concatenation is always faster
Confusing syntax of templates and concatenation
2. Which of the following is the correct Kotlin syntax to insert a variable name into a string using templates?
easy
A. "Hello, {name}!"
B. "Hello, \$name!"
C. "Hello, ${name}!"
D. "Hello, " + name + "!"
Solution
Step 1: Recall string template syntax
In Kotlin, to insert a variable inside a string, use $variable or ${variable} if expression is needed.
Step 2: Identify correct usage for variable insertion
For simple variables, $name works, but to be safe and clear, especially in complex expressions, ${name} is preferred.
Final Answer:
"Hello, ${name}!" -> Option C
Quick Check:
Variable insertion = ${name} [OK]
Hint: Use ${variable} for safe template insertion [OK]
Common Mistakes:
Using curly braces without $
Using + inside templates
Forgetting $ before variable
3. What will be the output of the following Kotlin code?
val age = 25
println("Age: " + age + " years")
println("Age: $age years")
medium
A. Age: 25 years
Age: 25 years
B. Age: 25 years
Age: $age years
C. Age: age years
Age: 25 years
D. Age: 25 years
Age: age years
Solution
Step 1: Evaluate first println with concatenation
"Age: " + age + " years" converts age (25) to string and concatenates, resulting in "Age: 25 years".
Step 2: Evaluate second println with template
"Age: $age years" inserts the value of age (25) directly, producing "Age: 25 years".
4. Identify the error in this Kotlin code snippet:
val score = 90
println("Your score is: $score") + " points"
medium
A. The + operator is used incorrectly outside println.
B. Variable score should be inside curly braces ${score}.
C. Missing semicolon after println statement.
D. String templates cannot be combined with + operator.
Solution
Step 1: Analyze the println statement
println("Your score is: $score") prints the string with the variable inserted correctly.
Step 2: Check the use of + operator
The + " points" is outside the println call, so it tries to add a string to the Unit result of println, causing an error.
Final Answer:
The + operator is used incorrectly outside println. -> Option A
Quick Check:
Operator placement error = B [OK]
Hint: Ensure + is inside println or use separate print [OK]
Common Mistakes:
Thinking templates need curly braces always
Adding + after println call
Expecting semicolon in Kotlin
5. You want to create a message showing a user's name and age using Kotlin string templates. The variables are val name = "Anna" and val age = 30. Which code snippet correctly produces the output Hello, Anna! You are 30 years old.?
hard
A. println("Hello, " + name + "! You are " + age + " years old.")
B. println("Hello, $name! You are $age years old.")
C. println("Hello, ${name}! You are ${age} years old.")
D. Both B and C
Solution
Step 1: Check concatenation approach
println("Hello, " + name + "! You are " + age + " years old.") uses concatenation with + operators and works but is less clean.
Step 2: Check template approaches
String templates using $variable and ${variable} syntax are both correct: simple $variable vs ${variable}, both valid and produce the same output.
Step 3: Determine correct options
Since both string template syntaxes produce the correct output, the option indicating both are correct is the answer.
Final Answer:
Both B and C -> Option D
Quick Check:
Templates with $ or ${} = D [OK]
Hint: Use $var or ${var} for templates; both work [OK]
Common Mistakes:
Thinking only one template syntax is correct
Confusing concatenation with templates
Missing quotes around strings
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.