What happens when you pass a Kotlin List to a Java method expecting a java.util.List?
Kotlin List is compiled to a java.util.List interface, so it can be passed directly to Java methods expecting java.util.List. This allows seamless interoperability without copying the collection.
Click to reveal answer
intermediate
How does Kotlin handle mutability when interoperating with Java collections?
Kotlin distinguishes between mutable and read-only collections. When Kotlin code receives a Java collection, it treats it as mutable because Java collections are mutable by default. Conversely, Kotlin's read-only collections are seen as interfaces without mutability guarantees in Java.
Click to reveal answer
intermediate
What is the difference between Kotlin's MutableList and Java's List in interop?
Kotlin's MutableList extends List and allows modification. Java's List interface is mutable by design. When Kotlin code calls Java methods returning List, it treats them as mutable, but Kotlin's read-only List interface does not guarantee mutability.
Click to reveal answer
beginner
How does Kotlin handle Java arrays when interoperating?
Java arrays are seen as Kotlin Array types. Kotlin provides special support to convert between Java arrays and Kotlin arrays without copying, enabling efficient interop.
Click to reveal answer
advanced
Why should you be careful when modifying collections passed between Kotlin and Java?
Because Kotlin distinguishes between mutable and read-only collections but Java collections are mutable by default, modifying collections passed between them can cause unexpected side effects. Always be aware of mutability contracts to avoid bugs.
Click to reveal answer
When Kotlin passes a List to Java, what type does Java see?
Ajava.util.List
BKotlin List
CArrayList only
DMutableList only
✗ Incorrect
Kotlin's List compiles to java.util.List, so Java sees it as java.util.List.
How does Kotlin treat Java collections it receives?
AAs mutable collections
BAs read-only collections
CAs immutable collections
DAs arrays
✗ Incorrect
Java collections are mutable by default, so Kotlin treats them as mutable.
What happens if you modify a Kotlin read-only list passed to Java?
AJava can modify it freely
BIt causes a compile error in Java
CIt depends on the underlying implementation
DIt is always immutable
✗ Incorrect
Read-only in Kotlin is a compile-time concept; Java can modify if the underlying collection supports it.
How are Java arrays seen in Kotlin?
AAs Kotlin Lists
BAs Kotlin Arrays
CAs MutableLists
DAs read-only collections
✗ Incorrect
Java arrays map directly to Kotlin Arrays for efficient interop.
Why is mutability important in Kotlin-Java collection interop?
ABecause Kotlin does not support Java collections
BBecause Java collections are always read-only
CBecause Kotlin collections are always immutable
DBecause mutability differences can cause unexpected bugs
✗ Incorrect
Mutability differences can cause bugs if not handled carefully.
Explain how Kotlin collections are represented when passed to Java and how mutability is handled.
Think about how Kotlin's read-only and mutable collections map to Java's collections.
You got /4 concepts.
Describe potential issues when modifying collections shared between Kotlin and Java.
Consider what happens if Java modifies a Kotlin read-only list.
You got /4 concepts.
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
Step 1: Understand Kotlin-Java collection interop
Kotlin collections can wrap Java collections directly, meaning no new copy is made.
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.
Final Answer:
Kotlin wraps the Java list directly without copying it. -> Option A
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
Step 1: Review Kotlin-Java mutable list interop
Kotlin mutable lists can be created by casting Java mutable lists directly without copying.
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.
Final Answer:
val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String> -> Option A
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
Step 1: Understand Kotlin wrapping Java list
The Kotlin List wraps the Java ArrayList directly, so changes to javaList reflect in kotlinList.
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.
Final Answer:
Kotlin
2 -> Option C
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
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.
Step 2: Trace mutation effect
Adding "Hello" to kotlinList modifies the underlying javaList, so javaList.size becomes 1.
Final Answer:
It compiles and javaList.size becomes 1. -> Option B
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
Step 1: Understand Kotlin-Java map interop
Kotlin mutable maps can directly wrap Java mutable maps like HashMap without copying.
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.
Final Answer:
val javaMap = java.util.HashMap<String, Int>()
val kotlinMap: MutableMap<String, Int> = javaMap -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.