A multiline string in Kotlin is a string enclosed in triple quotes ("""), allowing the string to span multiple lines without needing escape characters.
Click to reveal answer
beginner
What does the trimIndent() function do when used with multiline strings?
trimIndent() removes the common minimal indent from all lines in a multiline string, making the string's content aligned to the left without extra spaces.
Click to reveal answer
intermediate
How does trimIndent() help with code readability?
It allows you to indent multiline strings in your code to match the surrounding code style, but still outputs the string without those indents, keeping the string clean.
Click to reveal answer
beginner
Given this Kotlin code:
val text = """
Hello
World
""".trimIndent()
What will be the output of println(text)?
The output will be:
Hello
World
The trimIndent() removes the 4 spaces before each line.
Click to reveal answer
intermediate
Can trimIndent() remove different amounts of spaces on each line?
No, trimIndent() removes the smallest common indent from all lines. Lines with less indent are not changed, and lines with more indent lose only the common part.
Click to reveal answer
What symbol is used to start and end a multiline string in Kotlin?
A`
B"
C"""
D'
✗ Incorrect
Multiline strings in Kotlin are enclosed in triple double quotes: """.
What does trimIndent() do to a multiline string?
AAdds indentation to all lines
BConverts the string to uppercase
CRemoves all spaces from the string
DRemoves the smallest common indent from all lines
✗ Incorrect
trimIndent() removes the smallest common indent from all lines in the string.
If a multiline string has lines with different indents, how does trimIndent() behave?
ARemoves all indentation from every line
BRemoves the smallest common indent from all lines
CRemoves indentation only from the first line
DLeaves the string unchanged
✗ Incorrect
trimIndent() removes only the smallest common indent shared by all lines.
Why is trimIndent() useful when writing multiline strings in code?
AIt lets you indent strings in code for readability without affecting the actual string content
BIt encrypts the string content
CIt converts the string to a list
DIt adds line numbers to the string
✗ Incorrect
trimIndent() helps keep code readable by allowing indentation without adding spaces to the string.
What will this code print?
val s = """
Kotlin
Rocks
""".trimIndent()
println(s)
AKotlin
Rocks
B Kotlin
Rocks
CKotlin
Rocks
DskcoR
niltoK
✗ Incorrect
The smallest common indent is 2 spaces, so those are removed from each line. The second line keeps its extra 2 spaces.
Explain how trimIndent() works with multiline strings in Kotlin and why it is helpful.
Think about how indentation in code can affect string output.
You got /3 concepts.
Describe a situation where using a multiline string with trimIndent() would be better than a regular string.
Consider writing a message or template inside code.
You got /3 concepts.
Practice
(1/5)
1. What does the trimIndent() function do when used with multiline strings in Kotlin?
easy
A. It converts the string to uppercase.
B. It adds extra spaces at the beginning of each line.
C. It splits the string into a list of lines.
D. It removes the common leading whitespace from each line.
Solution
Step 1: Understand the purpose of trimIndent()
The function trimIndent() removes the common leading whitespace from every line in a multiline string.
Step 2: Effect on multiline strings
This makes the string output aligned neatly without extra indentation caused by code formatting.
Final Answer:
It removes the common leading whitespace from each line. -> Option D
Quick Check:
trimIndent() = removes leading spaces [OK]
Hint: trimIndent() cleans leading spaces for neat multiline strings [OK]
Common Mistakes:
Thinking trimIndent() adds spaces instead of removing
Confusing trimIndent() with uppercase conversion
Assuming trimIndent() splits strings into lines
2. Which of the following is the correct syntax to create a multiline string with trimmed indentation in Kotlin?
easy
A. val text = """Hello\nWorld""".trimIndent()
B. val text = "Hello\nWorld".trimIndent()
C. val text = '''Hello\nWorld'''.trimIndent()
D. val text = ""Hello\nWorld"".trimIndent()
Solution
Step 1: Identify multiline string syntax
Kotlin uses triple double quotes """ to create multiline strings.
Step 2: Apply trimIndent() correctly
Calling trimIndent() on the multiline string removes common leading spaces.
Final Answer:
val text = """Hello\nWorld""".trimIndent() -> Option A
Hint: Use triple quotes """ for multiline strings with trimIndent() [OK]
Common Mistakes:
Using single quotes instead of triple quotes
Trying to trimIndent() on a normal string
Wrong quote count causing syntax errors
3. What will be the output of the following Kotlin code?
val text = """
Kotlin
is
fun
""".trimIndent()
println(text)
medium
A. Kotlin
is
fun
B. Kotlin
is
fun
C. Kotlin
is
fun
D. Kotlin is fun
Solution
Step 1: Analyze the multiline string with indentation
The string has 4 spaces before each line inside the triple quotes.
Step 2: Effect of trimIndent()
trimIndent() removes the common 4-space indentation from all lines.
Final Answer:
Kotlin
is
fun -> Option C
Quick Check:
trimIndent() removes leading spaces = clean lines [OK]
Hint: trimIndent() removes common leading spaces from all lines [OK]
Common Mistakes:
Expecting original indentation to remain
Confusing trimIndent() with trim() which removes only ends
Thinking output is a single line string
4. Identify the error in this Kotlin code snippet using multiline strings and trimIndent():
val message = """
Hello
World
"".trimIndent()
println(message)
medium
A. Missing one double quote in the closing triple quotes.
B. The closing triple quotes should be """ not "".
C. No error; code runs and prints correctly.
D. Incorrect use of trimIndent() on a non-multiline string.
Solution
Step 1: Check triple quote syntax
Multiline strings require triple double quotes """ at start and end.
Step 2: Identify the closing quotes
The code uses only two double quotes "" at the end, which is invalid syntax.
Step 3: Correct the closing quotes
It should be """ to properly close the multiline string.
Final Answer:
The closing triple quotes should be """ not "". -> Option B
Quick Check:
Triple quotes must match at start and end [OK]
Hint: Always close multiline strings with triple quotes """ [OK]
Common Mistakes:
Using two quotes instead of three to close multiline strings
Assuming trimIndent() fixes quote errors
Confusing trimIndent() with trimming quotes
5. You want to create a multiline string in Kotlin that shows a list of fruits without extra indentation in the output. Which code snippet correctly achieves this using trimIndent()?
hard
A. val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits)
B. val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits)
C. val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits)
D. val fruits = "- Apple\n- Banana\n- Cherry".trimIndent()
println(fruits)
Solution
Step 1: Understand trimIndent() effect on indentation
trimIndent() removes the common leading spaces from all lines, aligning text properly.
Step 2: Check each option's indentation
val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits) has consistent 4-space indentation on each line, so trimIndent() will remove it neatly.
Step 3: Evaluate other options
val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits) has uneven indentation, so trimIndent() removes nothing and leaves extra spaces on some lines. val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits) has uneven indentation, so trimIndent() won't align all lines properly. val fruits = "- Apple\n- Banana\n- Cherry".trimIndent()
println(fruits) is a normal string, trimIndent() has no effect.
Final Answer:
val fruits = """
- Apple
- Banana
- Cherry
""".trimIndent()
println(fruits) -> Option A