0
0
Kotlinprogramming~20 mins

SAM conversions for Java interfaces in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SAM Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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)
ACompilation error
BHello from thread
CNo output
DRuntime exception
Attempts:
2 left
💡 Hint
SAM conversion lets you pass a lambda where a single-method Java interface is expected.
Predict Output
intermediate
1: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)
A[cherry, banana, apple]
B[apple, banana, cherry]
CCompilation error
D[banana, cherry, apple]
Attempts:
2 left
💡 Hint
The comparator sorts strings by descending length.
🔧 Debug
advanced
2: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") } }
AMyInterface has more than one abstract method, so SAM conversion is not applicable.
BKotlin does not support SAM conversions for interfaces declared in Kotlin.
CThe lambda syntax is incorrect for SAM conversion.
DMissing override keyword for doWork method.
Attempts:
2 left
💡 Hint
SAM conversion works only for interfaces with exactly one abstract method.
📝 Syntax
advanced
1: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"?
Aval c = Callable { "Done" }
Bval c = Callable<String> { return "Done" }
Cval c = Callable<String> { "Done"; println("Done") }
Dval c = Callable<String> { println("Done") }
Attempts:
2 left
💡 Hint
The lambda should return the value directly without explicit return keyword.
🚀 Application
expert
2: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?
A
val button = JButton("Click")
button.addActionListener(ActionListener { e -&gt; println("Button clicked") })
B
val button = JButton("Click")
button.addActionListener(ActionListener { println("Button clicked") })
C
val button = JButton("Click")
button.addActionListener { e -&gt; println("Button clicked") }
D
val button = JButton("Click")
button.addActionListener { println("Button clicked") }
Attempts:
2 left
💡 Hint
SAM conversion lets you pass a lambda with the event parameter for ActionListener.