Recall & Review
beginner
What is a multiline string in Kotlin?
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:<br>
val text = """
Hello
World
""".trimIndent()<br>What will be the output of println(text)?The output will be:<br>
Hello World<br>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?
✗ Incorrect
Multiline strings in Kotlin are enclosed in triple double quotes: """.
What does
trimIndent() do to a multiline string?✗ 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?✗ Incorrect
trimIndent() removes only the smallest common indent shared by all lines.Why is
trimIndent() useful when writing multiline strings in code?✗ Incorrect
trimIndent() helps keep code readable by allowing indentation without adding spaces to the string.What will this code print?<br>
val s = """
Kotlin
Rocks
""".trimIndent()
println(s)✗ 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.