Challenge - 5 Problems
String Comparison Master
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 comparing strings with equals()?
Consider the following Kotlin code snippet. What will it print?
Kotlin
fun main() { val a = "Hello" val b = "hello" println(a.equals(b)) println(a.equals(b, ignoreCase = true)) }
Attempts:
2 left
💡 Hint
Remember that equals() is case-sensitive by default unless ignoreCase is true.
✗ Incorrect
The first equals() call compares "Hello" and "hello" exactly, so it returns false. The second call ignores case, so it returns true.
❓ Predict Output
intermediate2:00remaining
What does compareTo() return when comparing "apple" and "banana"?
What is the output of this Kotlin code?
Kotlin
fun main() { val result = "apple".compareTo("banana") println(result) }
Attempts:
2 left
💡 Hint
compareTo returns a negative number if the first string is lexicographically less than the second.
✗ Incorrect
"apple" is lexicographically less than "banana" because the first characters 'a' (97) and 'b' (98) differ, so compareTo returns a negative number less than -1.
🔧 Debug
advanced2:00remaining
What error does this Kotlin code produce?
Examine this Kotlin code and identify the error it causes.
Kotlin
fun main() { val s1: String = null val s2 = "test" println(s1.equals(s2)) }
Attempts:
2 left
💡 Hint
In Kotlin, variables must be declared nullable to hold null.
✗ Incorrect
The variable s1 is declared as non-nullable String and assigned null, which is not allowed. This causes a compilation error.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code using compareTo with ignoreCase?
What will this Kotlin program print?
Kotlin
fun main() { val s1 = "Kotlin" val s2 = "kotlin" println(s1.compareTo(s2, ignoreCase = true)) println(s1.compareTo(s2)) }
Attempts:
2 left
💡 Hint
compareTo returns 0 if strings are equal ignoring case, otherwise a positive or negative number.
✗ Incorrect
With ignoreCase=true, "Kotlin" and "kotlin" are equal, so 0 is printed. Without ignoreCase, the difference of 'K' and 'k' ASCII codes is -32.
🧠 Conceptual
expert2:00remaining
How many items are in the resulting map after this Kotlin code?
What is the size of the map produced by this Kotlin code?
Kotlin
fun main() { val keys = listOf("a", "A", "b", "B") val map = keys.associateWith { it.lowercase() } val filteredMap = map.filterKeys { it.equals(it.lowercase(), ignoreCase = true) } println(filteredMap.size) }
Attempts:
2 left
💡 Hint
Check which keys equal their lowercase version ignoring case.
✗ Incorrect
Keys "a", "A", "b", and "B" all equal their lowercase ignoring case, so all 4 keys pass the filter. Therefore, filteredMap.size is 4.