0
0
Kotlinprogramming~5 mins

SAM conversions for Java interfaces in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does SAM stand for in the context of Java interfaces?
SAM stands for Single Abstract Method. It means an interface with exactly one abstract method.
Click to reveal answer
beginner
How does Kotlin support SAM conversions for Java interfaces?
Kotlin allows you to pass a lambda expression where a Java SAM interface is expected, automatically converting the lambda to an instance of that interface.
Click to reveal answer
beginner
Why are SAM conversions useful in Kotlin when working with Java interfaces?
They let you write concise and readable code by using lambdas instead of creating anonymous classes for Java interfaces with one abstract method.
Click to reveal answer
intermediate
Can Kotlin perform SAM conversions for Kotlin interfaces?
No, Kotlin only supports SAM conversions automatically for Java interfaces, not for Kotlin interfaces.
Click to reveal answer
beginner
Show a simple Kotlin example of SAM conversion with a Java interface Runnable.
Java interface Runnable has one method run(). In Kotlin, you can write: val r = Runnable { println("Running") } which creates a Runnable instance using a lambda.
Click to reveal answer
What is required for an interface to be eligible for SAM conversion in Kotlin?
AIt must have no methods.
BIt must have exactly one abstract method.
CIt must be a Kotlin interface.
DIt must have multiple abstract methods.
Can Kotlin perform SAM conversions automatically for Kotlin interfaces?
AOnly for interfaces with default methods.
BYes, always.
COnly if the interface is marked with a special annotation.
DNo, only for Java interfaces.
What Kotlin syntax is used to create a SAM instance from a Java interface?
AA lambda expression.
BAn anonymous class.
CA data class.
DA companion object.
Which Java interface is commonly used to demonstrate SAM conversion in Kotlin?
ACloneable
BSerializable
CRunnable
DComparator
What benefit does SAM conversion provide when calling Java code from Kotlin?
AMore concise and readable code using lambdas.
BFaster runtime performance.
CAutomatic null safety.
DBetter memory management.
Explain what SAM conversion is and how Kotlin uses it with Java interfaces.
Think about interfaces with one method and how lambdas replace anonymous classes.
You got /3 concepts.
    Describe the limitations of SAM conversions in Kotlin, especially regarding Kotlin interfaces.
    Consider which interfaces Kotlin can convert automatically and which it cannot.
    You got /3 concepts.