Challenge - 5 Problems
TrimIndent Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of trimIndent on a multiline string
What is the output of this Kotlin code snippet?
Kotlin
val text = """ Hello, Kotlin! Welcome to trimIndent. """.trimIndent() println(text)
Attempts:
2 left
💡 Hint
trimIndent removes the common minimal indent from all lines.
✗ Incorrect
The trimIndent function removes the smallest common indent from every line. The first line has a 4-space indent, while the second and third lines have 6-space indents. It removes 4 spaces from each, keeping the relative indentation.
❓ Predict Output
intermediate2:00remaining
Effect of trimIndent on uneven indentation
What will be printed by this Kotlin code?
Kotlin
val poem = """ Roses are red, Violets are blue, Kotlin is fun, And so are you. """.trimIndent() println(poem)
Attempts:
2 left
💡 Hint
trimIndent removes the smallest indent common to all lines.
✗ Incorrect
The smallest indent common to all lines is 8 spaces. After removing it, lines with more indentation keep their extra spaces.
🔧 Debug
advanced2:00remaining
Why does this trimIndent code produce unexpected output?
Consider this Kotlin code:
val message = """
Hello,
World!
""".trimIndent()
println(message)
Why does the output keep the extra spaces before 'World!'?
Attempts:
2 left
💡 Hint
Think about how trimIndent calculates the indent to remove.
✗ Incorrect
trimIndent finds the smallest indent common to all lines and removes that. Lines with more indentation keep their extra spaces.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this multiline string usage
Which option contains a syntax error when using trimIndent with multiline strings in Kotlin?
Kotlin
val text = """ Line one Line two Line three """.trimIndent()
Attempts:
2 left
💡 Hint
Check the closing triple quotes and method call syntax.
✗ Incorrect
Option A has only two double quotes to close the string, causing a syntax error. Triple quotes must be closed with exactly three double quotes.
🚀 Application
expert2:00remaining
How many lines does this trimmed multiline string contain?
Given this Kotlin code, how many lines does the resulting string contain after trimIndent is applied?
Kotlin
val data = """ { "name": "Alice", "age": 30 } """.trimIndent() val linesCount = data.lines().size println(linesCount)
Attempts:
2 left
💡 Hint
Count the lines inside the triple quotes after trimming indentation.
✗ Incorrect
The string has 5 lines: '{', ' "name": "Alice",', ' "age": 30', '}', and an empty line is not present. trimIndent does not remove lines, only indentation.