Bird
Raised Fist0
Kotlinprogramming~20 mins

Collections interop behavior in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. In Kotlin, when you use a Java List inside Kotlin code, what happens to the collection?
easy
A. Kotlin wraps the Java list directly without copying it.
B. Kotlin creates a new copy of the Java list.
C. Kotlin converts the Java list to a Kotlin Set.
D. Kotlin throws an error because Java collections are incompatible.

Solution

  1. Step 1: Understand Kotlin-Java collection interop

    Kotlin collections can wrap Java collections directly, meaning no new copy is made.
  2. Step 2: Identify behavior for Java List in Kotlin

    Using a Java List in Kotlin results in a wrapper around the original Java list, preserving changes.
  3. Final Answer:

    Kotlin wraps the Java list directly without copying it. -> Option A
  4. Quick Check:

    Kotlin wraps Java collections directly = D [OK]
Hint: Kotlin wraps Java collections directly, no copying needed [OK]
Common Mistakes:
  • Thinking Kotlin copies Java collections
  • Assuming Java collections convert to Kotlin sets
  • Believing Java collections cause errors in Kotlin
2. Which of the following Kotlin code snippets correctly creates a mutable Kotlin list from a Java ArrayList without copying?
easy
A. val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String>
B. val javaList = ArrayList<String>(); val kotlinList = javaList.toMutableList()
C. val javaList = ArrayList<String>(); val kotlinList = javaList.toList()
D. val javaList = ArrayList<String>(); val kotlinList = javaList.copy()

Solution

  1. Step 1: Review Kotlin-Java mutable list interop

    Kotlin mutable lists can be created by casting Java mutable lists directly without copying.
  2. Step 2: Analyze each option

    val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String> uses as MutableList<String> to wrap the Java ArrayList directly. toList() and toMutableList() create copies, and there is no copy() method.
  3. Final Answer:

    val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String> -> Option A
  4. Quick Check:

    Cast Java list to MutableList for no copy = C [OK]
Hint: Cast Java list to MutableList to avoid copying [OK]
Common Mistakes:
  • Using toList() or toMutableList() which copies
  • Trying to call non-existent copy() method
  • Not casting and expecting automatic conversion
3. What will be the output of the following Kotlin code?
val javaList = java.util.ArrayList<String>()
javaList.add("Kotlin")
val kotlinList: List<String> = javaList
println(kotlinList[0])
javaList.add("Java")
println(kotlinList.size)
medium
A. Kotlin 0
B. Kotlin 1
C. Kotlin 2
D. Compilation error

Solution

  1. Step 1: Understand Kotlin wrapping Java list

    The Kotlin List wraps the Java ArrayList directly, so changes to javaList reflect in kotlinList.
  2. Step 2: Trace the code output

    First print prints "Kotlin" (element at index 0). Then adding "Java" to javaList increases size to 2, so second print outputs 2.
  3. Final Answer:

    Kotlin 2 -> Option C
  4. Quick Check:

    Wrapped list reflects changes = A [OK]
Hint: Kotlin List wraps Java list, reflects changes instantly [OK]
Common Mistakes:
  • Assuming kotlinList is a copy and size stays 1
  • Expecting compilation error due to Java interop
  • Confusing index access with size
4. Consider this Kotlin code snippet:
val javaList = java.util.ArrayList<String>()
val kotlinList: MutableList<String> = javaList
kotlinList.add("Hello")
println(javaList.size)

What is the problem with this code?
medium
A. It compiles but javaList.size remains 0.
B. It compiles and javaList.size becomes 1.
C. It causes a runtime ClassCastException.
D. It causes a compilation error due to type mismatch.

Solution

  1. Step 1: Analyze Kotlin-Java mutable list assignment

    Assigning a Java ArrayList to a Kotlin MutableList variable works because Kotlin treats Java mutable collections as mutable Kotlin collections.
  2. Step 2: Trace mutation effect

    Adding "Hello" to kotlinList modifies the underlying javaList, so javaList.size becomes 1.
  3. Final Answer:

    It compiles and javaList.size becomes 1. -> Option B
  4. Quick Check:

    Mutable Kotlin list wraps Java list, changes reflect = A [OK]
Hint: Mutable Kotlin list wraps Java list; changes affect both [OK]
Common Mistakes:
  • Expecting runtime ClassCastException
  • Thinking javaList.size stays 0 after add
  • Assuming compilation error on assignment
5. You have a Java HashMap<String, Integer> and want to use it in Kotlin as a mutable map without copying. Which Kotlin code correctly achieves this and allows modifying the map?
hard
A. val javaMap = java.util.HashMap() val kotlinMap: Map = javaMap.toMap()
B. val javaMap = java.util.HashMap() val kotlinMap: Map = javaMap as Map
C. val javaMap = java.util.HashMap() val kotlinMap: MutableMap = javaMap.toMutableMap()
D. val javaMap = java.util.HashMap() val kotlinMap: MutableMap = javaMap

Solution

  1. Step 1: Understand Kotlin-Java map interop

    Kotlin mutable maps can directly wrap Java mutable maps like HashMap without copying.
  2. Step 2: Evaluate options for mutability and copying

    The direct assignment val kotlinMap: MutableMap<String, Int> = javaMap wraps the Java HashMap without copying and preserves mutability. In contrast, toMap() and casting to Map provide immutable access, while toMutableMap() creates a new mutable copy.
  3. Final Answer:

    val javaMap = java.util.HashMap<String, Int>() val kotlinMap: MutableMap<String, Int> = javaMap -> Option D
  4. Quick Check:

    Assign Java HashMap to MutableMap for no copy and mutability = B [OK]
Hint: Assign Java HashMap directly to MutableMap for no copy [OK]
Common Mistakes:
  • Using toMap() or toMutableMap() which copies
  • Casting to Map loses mutability
  • Assuming Kotlin creates a new map automatically
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard