0
0
Kotlinprogramming~20 mins

Multiline strings with trimIndent in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TrimIndent Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AHello,\nKotlin!\nWelcome to trimIndent.
B |Hello,\n | Kotlin!\n | Welcome to trimIndent.
C Hello,\n Kotlin!\n Welcome to trimIndent.
DHello,\n Kotlin!\n Welcome to trimIndent.
Attempts:
2 left
💡 Hint
trimIndent removes the common minimal indent from all lines.
Predict Output
intermediate
2: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)
ARoses are red,\nViolets are blue,\nKotlin is fun,\nAnd so are you.
BRoses are red,\n Violets are blue,\nRoses are red,\n And so are you.
CRoses are red,\n Violets are blue,\nKotlin is fun,\n And so are you.
D Roses are red,\n Violets are blue,\n Kotlin is fun,\n And so are you.
Attempts:
2 left
💡 Hint
trimIndent removes the smallest indent common to all lines.
🔧 Debug
advanced
2: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!'?
ABecause trimIndent only removes the smallest common indent from all lines, and 'World!' line has more indentation.
BBecause trimIndent removes all indentation from every line unconditionally.
CBecause trimIndent only removes indentation if lines start with a margin prefix like '|'.
DBecause trimIndent removes trailing spaces but not leading spaces.
Attempts:
2 left
💡 Hint
Think about how trimIndent calculates the indent to remove.
📝 Syntax
advanced
2: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()
A
val text = """
    Line one
    Line two
    Line three
"".trimIndent()
B
)(tnednImirt."""
eerht eniL    
owt eniL    
eno eniL    
""" = txet lav
C
val text = """
    Line one
    Line two
    Line three
""".trimIndent()
D
al text = """
    Line one
    Line two
    Line three
""".trimIndent()
Attempts:
2 left
💡 Hint
Check the closing triple quotes and method call syntax.
🚀 Application
expert
2: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)
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Count the lines inside the triple quotes after trimming indentation.