It extracts a part of a string from a start index up to, but not including, an end index, returning a new string.
Click to reveal answer
beginner
How does the split method work on a string?
It divides the string into parts based on a delimiter and returns a list of substrings.
Click to reveal answer
beginner
What is the purpose of the trim method in Kotlin strings?
It removes any whitespace characters from the start and end of the string.
Click to reveal answer
beginner
Given val text = " Hello Kotlin ", what does text.trim() return?
It returns the string "Hello Kotlin" without the spaces at the start and end.
Click to reveal answer
beginner
How would you get the substring "Kotlin" from val text = "Hello Kotlin"?
Use text.substring(6) to get "Kotlin" starting from index 6 to the end.
Click to reveal answer
What does "apple,banana,orange".split(",") return?
A"applebananaorange"
B[",", ",", ","]
C["apple,banana,orange"]
D["apple", "banana", "orange"]
✗ Incorrect
The split method divides the string at each comma, returning a list of the parts.
What is the result of " Kotlin ".trim()?
A" Kotlin "
B" Kotlin "
C"Kotlin"
D""
✗ Incorrect
Trim removes spaces from the start and end, leaving just "Kotlin".
Which substring call extracts "cat" from "concatenate"?
Asubstring(3, 6)
Bsubstring(0, 3)
Csubstring(4, 7)
Dsubstring(5, 8)
✗ Incorrect
Characters at indexes 3 to 5 are 'c', 'a', 't' which form "cat".
What does "one two three".split(" ") produce?
A["one two three"]
B["one", "two", "three"]
C"onetwothree"
D[" ", " "]
✗ Incorrect
Splitting by space divides the string into words.
If val s = " abc ", what does s.substring(2, 5) return?
A"abc"
B" a"
C"bc "
D" "
✗ Incorrect
Indexes 2 to 4 are 'a', 'b', 'c', so substring returns "abc".
Explain how to use substring, split, and trim methods on strings in Kotlin with simple examples.
Think about cutting, breaking, and cleaning a string.
You got /4 concepts.
Describe a real-life situation where you might use split and trim methods together on a string.
Imagine cleaning and breaking a sentence typed by a user.
You got /3 concepts.
Practice
(1/5)
1. What does the Kotlin trim() method do when called on a string?
easy
A. Converts the string to uppercase
B. Splits the string into parts based on spaces
C. Extracts a part of the string by index
D. Removes spaces from the start and end of the string
Solution
Step 1: Understand the purpose of trim()
The trim() method removes whitespace characters from the beginning and end of a string.
Step 2: Compare with other string methods
Unlike split() or substring(), trim() does not split or extract parts but cleans spaces around the string.
Final Answer:
Removes spaces from the start and end of the string -> Option D
Quick Check:
trim() removes outer spaces [OK]
Hint: trim() cleans spaces only at string edges [OK]
Common Mistakes:
Thinking trim() splits the string
Confusing trim() with substring()
Assuming trim() changes case
2. Which of the following is the correct Kotlin syntax to get a substring from index 2 to 5 (exclusive) of a string text?
easy
A. text.substring(2..5)
B. text.subString(2, 5)
C. text.substring(2, 5)
D. text.substring(2, 6)
Solution
Step 1: Recall Kotlin substring syntax
The correct method call is substring(startIndex, endIndex) where endIndex is exclusive.
Step 2: Check each option
text.substring(2..5) uses a range which is invalid for substring. text.subString(2, 5) has wrong capitalization. text.substring(2, 6) uses 6 as endIndex which extracts more characters than asked.
Final Answer:
text.substring(2, 5) -> Option C
Quick Check:
substring(start, end) uses exclusive end [OK]
Hint: Use substring(start, end) with end exclusive [OK]
Common Mistakes:
Using range syntax inside substring
Wrong method name capitalization
Confusing inclusive vs exclusive end index
3. What is the output of the following Kotlin code?
val data = " apple, banana , cherry "
val fruits = data.trim().split(",")
println(fruits[1].trim())
medium
A. ananab
B. banana
C. anana
D. banan
Solution
Step 1: Apply trim() on the original string
Calling trim() removes spaces at start and end, so string becomes "apple, banana , cherry".
Step 2: Split by comma and trim the second element
Splitting by "," gives ["apple", " banana ", " cherry"]. Then trimming the second element " banana " removes spaces, resulting in "banana".
Final Answer:
banana -> Option B
Quick Check:
trim() cleans spaces after split [OK]
Hint: Trim after split to remove spaces around parts [OK]
Common Mistakes:
Not trimming after split, leaving spaces
Assuming split trims automatically
Confusing index positions in list
4. Find the error in this Kotlin code snippet:
val sentence = "Hello World"
val part = sentence.substring(6, 5)
println(part)
medium
A. substring end index is less than start index causing error
B. substring method requires only one argument
C. substring indexes are inclusive, so 6 to 5 is valid
D. No error, output is "World"
Solution
Step 1: Check substring parameters
substring(startIndex, endIndex) requires startIndex ≤ endIndex. Here, startIndex=6 and endIndex=5, which is invalid.
Step 2: Understand Kotlin substring behavior
This causes an exception because the end index is less than the start index.
Final Answer:
substring end index is less than start index causing error -> Option A
Quick Check:
substring end must be ≥ start [OK]
Hint: End index must be >= start index in substring [OK]
Common Mistakes:
Assuming substring indexes are inclusive
Using end index less than start index
Thinking substring accepts one argument only
5. Given a string input = " red, green , blue , yellow ", write Kotlin code to create a list of trimmed color names without spaces. Which code snippet correctly achieves this?
hard
A. input.split(",").map { it.trim() }
B. input.trim().split(",")
C. input.split(",").trim()
D. input.trim().map { it.split(",") }
Solution
Step 1: Split the string by commas
Using split(",") breaks the string into parts including spaces.
Step 2: Trim spaces from each part
Using map { it.trim() } removes spaces from each color name, producing a clean list.
Step 3: Evaluate other options
input.trim().split(",") trims only the ends before splitting, so inner spaces remain. input.split(",").trim() tries to trim a list, which is invalid. input.trim().map { it.split(",") } trims the whole string then maps incorrectly.
Final Answer:
input.split(",").map { it.trim() } -> Option A
Quick Check:
Split then trim each part for clean list [OK]
Hint: Split first, then trim each piece with map [OK]
Common Mistakes:
Trimming before split only removes outer spaces
Trying to trim a list directly
Incorrect chaining of methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.