Challenge - 5 Problems
Collections Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kotlin List to Java List Interop
What is the output of the following Kotlin code when run on the JVM?
Kotlin
val kotlinList = listOf("a", "b", "c") val javaList: java.util.List<String> = kotlinList println(javaList.javaClass.name)
Attempts:
2 left
💡 Hint
Think about how Kotlin's listOf creates a fixed-size list backed by an array.
✗ Incorrect
Kotlin's listOf creates a read-only list backed by a Java array, which is represented as java.util.Arrays$ArrayList on the JVM.
❓ Predict Output
intermediate2:00remaining
Modifying Kotlin MutableList via Java List Reference
Given the Kotlin code below, what will be the output?
Kotlin
val mutableList = mutableListOf(1, 2, 3) val javaList: java.util.List<Int> = mutableList javaList.add(4) println(mutableList)
Attempts:
2 left
💡 Hint
MutableList in Kotlin is backed by a Java ArrayList, so changes via Java List reflect in Kotlin list.
✗ Incorrect
The mutableListOf returns a Kotlin MutableList backed by a Java ArrayList. Adding an element via the Java List reference modifies the same underlying list.
🔧 Debug
advanced2:00remaining
Why does this Kotlin to Java Map interop code throw an exception?
Consider this Kotlin code snippet that passes a Kotlin Map to a Java method expecting a java.util.Map. The Java method tries to modify the map but throws an exception. Why?
Kotlin
fun main() { val kotlinMap = mapOf("key" to "value") modifyMap(kotlinMap) } fun modifyMap(map: java.util.Map<String, String>) { map.put("newKey", "newValue") }
Attempts:
2 left
💡 Hint
Check the mutability of the Kotlin map created by mapOf.
✗ Incorrect
Kotlin's mapOf creates an immutable map. When passed to Java, it is seen as a java.util.Map but does not support modification methods like put, causing UnsupportedOperationException.
📝 Syntax
advanced2:00remaining
Correct Kotlin syntax to convert Java List to Kotlin MutableList
Which of the following Kotlin code snippets correctly converts a java.util.List to a Kotlin MutableList?
Attempts:
2 left
💡 Hint
Look for the idiomatic Kotlin way to copy a Java list into a mutable Kotlin list.
✗ Incorrect
The toMutableList() extension function creates a new Kotlin MutableList from any Iterable, including Java lists. Casting (option A) is unsafe and may fail at runtime.
🚀 Application
expert2:00remaining
Predict the size of a Kotlin Set after Java interop operations
Given the Kotlin code below, what is the size of the set after all operations?
Kotlin
val kotlinSet = mutableSetOf("a", "b", "c") val javaSet: java.util.Set<String> = kotlinSet javaSet.add("d") javaSet.remove("b") println(kotlinSet.size)
Attempts:
2 left
💡 Hint
Consider how Kotlin's mutableSetOf is backed and how Java Set operations affect it.
✗ Incorrect
Kotlin's mutableSetOf returns a LinkedHashSet, which implements the mutable methods of java.util.Set including add and remove. Thus, add("d") succeeds (size 4), then remove("b") succeeds (size 3).