Challenge - 5 Problems
String Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code?
Consider the following Kotlin code that manipulates strings. What will it print?
Kotlin
fun main() { val text = "Kotlin" val result = text.substring(1, 4) + text.last() println(result) }
Attempts:
2 left
💡 Hint
Remember substring(start, end) includes start but excludes end index.
✗ Incorrect
The substring(1,4) extracts characters at indices 1, 2, and 3: 'o', 't', 'l'. The last character is 'n'. Concatenating gives 'otl' + 'n' = 'otln'.
🧠 Conceptual
intermediate1:30remaining
Why is string immutability important in Kotlin?
Which of the following best explains why strings in Kotlin are immutable?
Attempts:
2 left
💡 Hint
Think about what happens when many parts of a program use the same string.
✗ Incorrect
Immutable strings mean once created, they cannot change. This allows safe sharing across threads without unexpected changes or conflicts.
🔧 Debug
advanced2:00remaining
What error does this Kotlin code produce?
Examine the code below. What error will it cause when run?
Kotlin
fun main() { val s = "Hello" val c = s[5] println(c) }
Attempts:
2 left
💡 Hint
Remember string indices start at 0 and go up to length-1.
✗ Incorrect
The string "Hello" has indices 0 to 4. Accessing s[5] is out of range, causing IndexOutOfBoundsException.
📝 Syntax
advanced1:30remaining
Which option correctly reverses a string in Kotlin?
Choose the Kotlin code snippet that correctly reverses the string "abcde".
Attempts:
2 left
💡 Hint
Check the exact function name and usage for reversing strings in Kotlin.
✗ Incorrect
The correct function is reversed() with parentheses. reverse() does not exist for String, reversed is a function, not a property, and reverse() is not a top-level function.
🚀 Application
expert2:00remaining
How many items are in the resulting list?
What is the size of the list produced by splitting the string "one,two,,four," by comma in Kotlin?
Kotlin
fun main() { val input = "one,two,,four," val parts = input.split(",") println(parts.size) }
Attempts:
2 left
💡 Hint
Consider how split handles consecutive delimiters and trailing delimiters.
✗ Incorrect
Splitting "one,two,,four," by comma produces: ["one", "two", "", "four", ""]. There are 5 items including empty strings for consecutive and trailing commas.