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?
✗ Incorrect
SAM conversion works only for interfaces with exactly one abstract method.
Can Kotlin perform SAM conversions automatically for Kotlin interfaces?
✗ Incorrect
Kotlin supports SAM conversions automatically only for Java interfaces, not Kotlin interfaces.
What Kotlin syntax is used to create a SAM instance from a Java interface?
✗ Incorrect
You use a lambda expression to create an instance of a Java SAM interface in Kotlin.
Which Java interface is commonly used to demonstrate SAM conversion in Kotlin?
✗ Incorrect
Runnable has a single abstract method run(), making it a classic example for SAM conversion.
What benefit does SAM conversion provide when calling Java code from Kotlin?
✗ Incorrect
SAM conversions let you write shorter and clearer code by using lambdas instead of anonymous classes.
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.