📖 Scenario: Imagine you are creating a simple contact list app. You need to store names and then check if a certain name is in the list. Handling strings correctly helps you find names easily and avoid mistakes.
🎯 Goal: You will build a small Kotlin program that stores a list of names, sets a name to search for, checks if that name is in the list, and then prints a message showing the result.
📋 What You'll Learn
Create a list of names with exact values
Create a variable to hold the name to search
Use a condition to check if the name is in the list
Print a message showing if the name was found or not
💡 Why This Matters
🌍 Real World
String handling is used in apps to search contacts, filter messages, or check user input.
💼 Career
Many programming jobs require working with text data, so knowing how to handle strings is a key skill.
Progress0 / 4 steps
1
Create a list of names
Create a list called names with these exact values: "Alice", "Bob", "Charlie", "Diana", "Eve"
Kotlin
Hint
Use listOf to create a list of strings.
2
Set the name to search
Create a variable called searchName and set it to the string "Charlie"
Kotlin
Hint
Use val to create a variable holding the string.
3
Check if the name is in the list
Create a variable called found and set it to the result of checking if searchName is in the names list using the contains method
Kotlin
Hint
Use contains to check if a list has a string.
4
Print the result
Write a println statement that prints "Found Charlie in the list!" if found is true, otherwise prints "Charlie was not found."
Kotlin
Hint
Use an if statement to print different messages based on found.
Practice
(1/5)
1. Why is string handling important in Kotlin programming?
easy
A. It controls the hardware devices directly.
B. It speeds up the computer's processor.
C. It helps us work with text like messages and user input.
D. It manages memory allocation automatically.
Solution
Step 1: Understand the role of strings in programs
Strings represent text, which is essential for messages, input, and output.
Step 2: Identify why handling strings matters
Handling strings allows programs to display messages and process user input effectively.
Final Answer:
It helps us work with text like messages and user input. -> Option C
Quick Check:
String handling = working with text [OK]
Hint: Strings = text handling in programs [OK]
Common Mistakes:
Confusing string handling with hardware control
Thinking string handling speeds up processors
Mixing string handling with memory management
2. Which of the following is the correct way to create a string variable in Kotlin?
easy
A. val name = Kotlin
B. val name = "Kotlin"
C. var name := "Kotlin"
D. string name = 'Kotlin'
Solution
Step 1: Recall Kotlin string declaration syntax
In Kotlin, strings are declared with val or var and use double quotes for text.
Step 2: Check each option
val name = "Kotlin" uses val and double quotes correctly. Others have syntax errors or missing quotes.
Final Answer:
val name = "Kotlin" -> Option B
Quick Check:
String variable = val + "text" [OK]
Hint: Use val/var and double quotes for strings [OK]
Common Mistakes:
Using single quotes for strings
Missing quotes around text
Using incorrect assignment operators
3. What will be the output of this Kotlin code?
val greeting = "Hello"
val name = "Anna"
println(greeting + ", " + name + "!")
medium
A. Hello, Anna!
B. Hello Anna!
C. Hello,Anna!
D. Hello Anna
Solution
Step 1: Understand string concatenation in Kotlin
The + operator joins strings exactly as written, including commas and spaces.
Step 2: Combine the strings step-by-step
"Hello" + ", " + "Anna" + "!" results in "Hello, Anna!" with comma and space.
Final Answer:
Hello, Anna! -> Option A
Quick Check:
Concatenate strings with + = Hello, Anna! [OK]
Hint: Check spaces and punctuation in concatenation [OK]
Common Mistakes:
Ignoring spaces after commas
Missing punctuation in output
Confusing + with other operators
4. Find the error in this Kotlin code that tries to print a user's name:
val userName = 'John'
println("User: " + userName)
medium
A. Using single quotes instead of double quotes for string
B. Missing plus sign for concatenation
C. Incorrect variable name in println
D. No error, code is correct
Solution
Step 1: Check string declaration syntax
Kotlin strings must use double quotes, not single quotes.
Step 2: Identify the error in userName assignment
Using single quotes 'John' causes a syntax error; it should be "John".
Final Answer:
Using single quotes instead of double quotes for string -> Option A
Quick Check:
Strings need double quotes in Kotlin [OK]
Hint: Strings always use double quotes "" [OK]
Common Mistakes:
Using single quotes for strings
Assuming single quotes work like in other languages
Ignoring syntax errors on string literals
5. You want to create a greeting message that includes a user's name and age in Kotlin. Which code correctly uses string templates to do this?
val name = "Sam"
val age = 25
val message = ???
println(message)
hard
A. val message = "Hello, $ name! You are $ age years old."
B. val message = 'Hello, $name! You are $age years old.'
C. val message = "Hello, " + name + "! You are " + age + " years old."
D. val message = "Hello, ${name}! You are ${age} years old."
Solution
Step 1: Understand Kotlin string templates
Kotlin uses $variable or ${expression} inside double quotes to insert values.
Step 2: Compare options for correct template usage
val message = "Hello, ${name}! You are ${age} years old." uses ${name} and ${age} correctly inside the string for clarity and safety.
Final Answer:
val message = "Hello, ${name}! You are ${age} years old." -> Option D
Quick Check:
Use ${} for variables in strings [OK]
Hint: Use ${} for variables inside strings [OK]
Common Mistakes:
Using single quotes for strings
Not using ${} for expressions
Concatenating strings manually instead of templates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.