0
0
Kotlinprogramming~20 mins

Collections interop behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Collections Interop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
Ajava.util.Arrays$ArrayList
Bjava.util.ArrayList
Cjava.util.Collections$UnmodifiableRandomAccessList
Dkotlin.collections.ArraysKt___ArraysJvmKt$asList
Attempts:
2 left
💡 Hint
Think about how Kotlin's listOf creates a fixed-size list backed by an array.
Predict Output
intermediate
2: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)
ARuntime exception
B[1, 2, 3]
C[1, 2, 3, 4]
DCompilation error
Attempts:
2 left
💡 Hint
MutableList in Kotlin is backed by a Java ArrayList, so changes via Java List reflect in Kotlin list.
🔧 Debug
advanced
2: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")
}
AKotlin maps cannot be passed to Java methods due to type erasure.
BThe Java method signature is incorrect and causes a ClassCastException.
CThe JVM does not allow modification of any map passed from Kotlin to Java.
DThe Kotlin mapOf returns an immutable map that throws UnsupportedOperationException on put.
Attempts:
2 left
💡 Hint
Check the mutability of the Kotlin map created by mapOf.
📝 Syntax
advanced
2: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?
Aval kotlinMutableList = javaList.toMutableList()
Bval kotlinMutableList = javaList as MutableList<String>
Cval kotlinMutableList = javaList.toList().toMutableList()
Dval kotlinMutableList = mutableListOf(javaList)
Attempts:
2 left
💡 Hint
Look for the idiomatic Kotlin way to copy a Java list into a mutable Kotlin list.
🚀 Application
expert
2: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)
A4
B3
C2
DRuntime exception
Attempts:
2 left
💡 Hint
Consider how Kotlin's mutableSetOf is backed and how Java Set operations affect it.