0
0
Kotlinprogramming~20 mins

StringBuilder for performance in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StringBuilder Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using StringBuilder?
Consider the following Kotlin code snippet. What will it print?
Kotlin
fun main() {
    val sb = StringBuilder()
    for (i in 1..3) {
        sb.append(i)
        sb.append(",")
    }
    println(sb.toString())
}
A1,2,3,
B1, 2, 3,
C1,2,3
D123,
Attempts:
2 left
💡 Hint
Look carefully at what is appended inside the loop.
Predict Output
intermediate
2:00remaining
What is the final string length after these StringBuilder operations?
Given this Kotlin code, what is the length of the string in sb after execution?
Kotlin
fun main() {
    val sb = StringBuilder("Hello")
    sb.append(" World")
    sb.delete(5, 6)
    println(sb.length)
}
A6
B11
C5
D10
Attempts:
2 left
💡 Hint
Deleting one character reduces length by one.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code throw an exception?
This Kotlin code throws an exception. What is the cause?
Kotlin
fun main() {
    val sb = StringBuilder("abc")
    sb.delete(1, 5)
    println(sb.toString())
}
ANo exception, output is "a"
BStringIndexOutOfBoundsException because end index is greater than string length
CNullPointerException because sb is null
DIllegalArgumentException because start index is greater than end index
Attempts:
2 left
💡 Hint
Check the valid range for delete method indices.
🧠 Conceptual
advanced
2:00remaining
Why is StringBuilder preferred over String concatenation in loops?
Which option best explains why using StringBuilder is better than using String concatenation inside loops in Kotlin?
AStringBuilder modifies the string in place, avoiding creating many temporary String objects
BString concatenation is faster because it uses immutable strings
CString concatenation uses less memory than StringBuilder
DStringBuilder automatically sorts the characters for better performance
Attempts:
2 left
💡 Hint
Think about how strings are stored and changed in memory.
Predict Output
expert
2:00remaining
What is the output of this Kotlin code using StringBuilder and insert?
Analyze this Kotlin code and select the exact output it produces.
Kotlin
fun main() {
    val sb = StringBuilder("Kotlin")
    sb.insert(3, " is")
    sb.insert(0, "I think")
    println(sb.toString())
}
AI thinkK isotlin
BI thinkKotlin is
CI thinkKot islin
Dsi niltoKkniht I
Attempts:
2 left
💡 Hint
Remember insert shifts characters to the right.