0
0
Kotlinprogramming~5 mins

Collections interop behavior in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
How does Kotlin treat Java collections it receives?
AAs mutable collections
BAs read-only collections
CAs immutable collections
DAs arrays
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
How are Java arrays seen in Kotlin?
AAs Kotlin Lists
BAs Kotlin Arrays
CAs MutableLists
DAs read-only collections
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
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.