Challenge - 5 Problems
SAM Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of SAM conversion with Runnable
What is the output of this Kotlin code using SAM conversion for a Java Runnable interface?
Kotlin
val thread = Thread(Runnable { println("Hello from thread") }) thread.start() Thread.sleep(100)
Attempts:
2 left
💡 Hint
SAM conversion lets you pass a lambda where a single-method Java interface is expected.
✗ Incorrect
The lambda is converted to a Runnable instance. When thread.start() runs, it prints the message.
❓ Predict Output
intermediate1:30remaining
SAM conversion with Comparator interface
What does this Kotlin code print when using SAM conversion for Java's Comparator interface?
Kotlin
val list = listOf("apple", "banana", "cherry") val sorted = list.sortedWith(Comparator { a, b -> b.length - a.length }) println(sorted)
Attempts:
2 left
💡 Hint
The comparator sorts strings by descending length.
✗ Incorrect
The comparator compares string lengths in reverse order, so 'banana' (6), 'cherry' (6), then 'apple' (5). Since 'banana' and 'cherry' have same length, their order is stable.
🔧 Debug
advanced2:00remaining
Why does this SAM conversion fail?
This Kotlin code tries to use SAM conversion with a Java interface but fails. Why?
interface MyInterface {
fun doWork()
fun extra()
}
fun test() {
val obj = MyInterface { println("Work done") }
}
Attempts:
2 left
💡 Hint
SAM conversion works only for interfaces with exactly one abstract method.
✗ Incorrect
SAM conversion applies only to Java interfaces with a single abstract method. Here, MyInterface has two abstract methods, so Kotlin cannot convert the lambda.
📝 Syntax
advanced1:30remaining
Correct SAM conversion syntax for Java Callable
Which option correctly uses SAM conversion in Kotlin for the Java Callable interface to return the string "Done"?
Attempts:
2 left
💡 Hint
The lambda should return the value directly without explicit return keyword.
✗ Incorrect
Option A correctly uses SAM conversion with a lambda returning "Done". Option A has an invalid return statement inside lambda. Option A returns Unit, not String. Option A returns last expression which is println result (Unit).
🚀 Application
expert2:30remaining
Using SAM conversion with Java's ActionListener in Kotlin
You want to create a Java Swing JButton in Kotlin and add an ActionListener using SAM conversion. Which code snippet correctly prints "Button clicked" when the button is pressed?
Attempts:
2 left
💡 Hint
SAM conversion lets you pass a lambda with the event parameter for ActionListener.
✗ Incorrect
Option C uses SAM conversion with a lambda that takes the event parameter 'e' and prints the message. Option C misses the event parameter, which is required. Option C and C use explicit ActionListener constructor which is valid but verbose; Option C is invalid syntax because ActionListener is a functional interface, not a constructor taking a lambda directly.