Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the purpose of trimIndent()

    The function trimIndent() removes the common leading whitespace from every line in a multiline string.
  2. Step 2: Effect on multiline strings

    This makes the string output aligned neatly without extra indentation caused by code formatting.
  3. Final Answer:

    It removes the common leading whitespace from each line. -> Option D
  4. 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

  1. Step 1: Identify multiline string syntax

    Kotlin uses triple double quotes """ to create multiline strings.
  2. Step 2: Apply trimIndent() correctly

    Calling trimIndent() on the multiline string removes common leading spaces.
  3. Final Answer:

    val text = """Hello\nWorld""".trimIndent() -> Option A
  4. Quick Check:

    Triple quotes + trimIndent() = correct syntax [OK]
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

  1. Step 1: Analyze the multiline string with indentation

    The string has 4 spaces before each line inside the triple quotes.
  2. Step 2: Effect of trimIndent()

    trimIndent() removes the common 4-space indentation from all lines.
  3. Final Answer:

    Kotlin is fun -> Option C
  4. 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

  1. Step 1: Check triple quote syntax

    Multiline strings require triple double quotes """ at start and end.
  2. Step 2: Identify the closing quotes

    The code uses only two double quotes "" at the end, which is invalid syntax.
  3. Step 3: Correct the closing quotes

    It should be """ to properly close the multiline string.
  4. Final Answer:

    The closing triple quotes should be """ not "". -> Option B
  5. 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

  1. Step 1: Understand trimIndent() effect on indentation

    trimIndent() removes the common leading spaces from all lines, aligning text properly.
  2. 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.
  3. 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.
  4. Final Answer:

    val fruits = """ - Apple - Banana - Cherry """.trimIndent() println(fruits) -> Option A
  5. Quick Check:

    Consistent indentation + trimIndent() = neat output [OK]
Hint: Use consistent indentation inside triple quotes before trimIndent() [OK]
Common Mistakes:
  • Using uneven indentation causing partial trimming
  • Not using triple quotes for multiline strings
  • Expecting trimIndent() to fix uneven spaces
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard