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.
✗ Incorrect
SAM conversion works only for interfaces with exactly one abstract method.
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.
✗ 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?
AA lambda expression.
BAn anonymous class.
CA data class.
DA companion object.
✗ 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?
ACloneable
BSerializable
CRunnable
DComparator
✗ 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?
AMore concise and readable code using lambdas.
BFaster runtime performance.
CAutomatic null safety.
DBetter memory management.
✗ 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.
Practice
(1/5)
1.
What does SAM conversion allow you to do in Kotlin when working with Java interfaces?
easy
A. Automatically convert Kotlin functions to Java classes
B. Convert Java interfaces to Kotlin data classes
C. Use multiple methods in a Java interface as lambdas
D. Use a lambda expression instead of an anonymous class for interfaces with one method
Solution
Step 1: Understand SAM conversion purpose
SAM conversion lets Kotlin treat Java interfaces with a single abstract method as functional interfaces.
Step 2: Identify how it simplifies code
This allows using lambda expressions instead of verbose anonymous classes for such interfaces.
Final Answer:
Use a lambda expression instead of an anonymous class for interfaces with one method -> Option D
Quick Check:
SAM conversion = Lambda for single-method Java interfaces [OK]
Hint: Remember: SAM = Single Abstract Method = one lambda [OK]
Common Mistakes:
Thinking SAM works for interfaces with multiple methods
Confusing SAM conversion with Kotlin function to Java class conversion
Assuming SAM converts Java interfaces to Kotlin classes
2.
Which of the following is the correct Kotlin syntax to use SAM conversion with the Java Runnable interface?
val r = ???
easy
A. Runnable { println("Running") }
B. Runnable.run { println("Running") }
C. Runnable.run() -> println("Running")
D. Runnable.run { -> println("Running") }
Solution
Step 1: Recall SAM conversion syntax for Runnable
Runnable is a Java interface with one method run(), so Kotlin allows lambda syntax: Runnable { ... }
Identify the error in this Kotlin code using SAM conversion with a Java interface ActionListener:
val listener = ActionListener { event ->
println("Clicked")
}
button.addActionListener(listener)
medium
A. SAM conversion works only if the interface is a Java interface with one method
B. The lambda parameter name must be e, not event
C. ActionListener is a Kotlin interface, so SAM conversion does not apply
D. You must use an anonymous object instead of a lambda
Solution
Step 1: Check if ActionListener is a Java interface with one method
ActionListener is a Java interface with a single method actionPerformed(ActionEvent).
Step 2: Verify SAM conversion applicability
SAM conversion applies only to Java interfaces with exactly one abstract method, so it should work here.
Step 3: Identify the actual error
If the code does not compile, it might be because the Kotlin compiler does not recognize ActionListener as a SAM interface due to missing Java interop or import.
Final Answer:
SAM conversion works only if the interface is a Java interface with one method -> Option A
Quick Check:
SAM conversion requires Java single-method interface [OK]
Hint: SAM only works with Java interfaces having one abstract method [OK]
Common Mistakes:
Assuming Kotlin interfaces support SAM conversion
Thinking parameter name must be fixed
Believing anonymous objects are always required
5.
You want to use SAM conversion to simplify this Java interface usage in Kotlin:
public interface Task {
void execute(int times);
}
Which Kotlin lambda expression correctly uses SAM conversion for Task?
hard
A. Task { println("Executed times") }
B. Task { times -> println("Executed $times times") }
C. Task.execute { times -> println("Executed $times times") }
D. Task.run { times -> println("Executed $times times") }
Solution
Step 1: Confirm Task is a Java interface with one method
Task has one method execute(int times), so SAM conversion applies.
Step 2: Write correct lambda syntax for SAM conversion
The lambda must accept one parameter (times) matching the method signature: Task { times -> ... }
Step 3: Check each option
Task { times -> println("Executed $times times") } correctly uses Task { times -> println(...) }. Others misuse method calls or omit parameter.
Final Answer:
Task { times -> println("Executed $times times") } -> Option B
Quick Check:
SAM lambda matches method parameters = Task { times -> println("Executed $times times") } [OK]
Hint: Match lambda parameters to single method parameters exactly [OK]
Common Mistakes:
Calling method name explicitly in lambda
Omitting lambda parameter when method requires one
Using wrong method name in lambda expression
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.