Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand SAM conversion purpose

    SAM conversion lets Kotlin treat Java interfaces with a single abstract method as functional interfaces.
  2. Step 2: Identify how it simplifies code

    This allows using lambda expressions instead of verbose anonymous classes for such interfaces.
  3. Final Answer:

    Use a lambda expression instead of an anonymous class for interfaces with one method -> Option D
  4. 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

  1. Step 1: Recall SAM conversion syntax for Runnable

    Runnable is a Java interface with one method run(), so Kotlin allows lambda syntax: Runnable { ... }
  2. Step 2: Check each option's syntax

    Runnable { println("Running") } correctly uses Runnable { println("Running") }. Others misuse method calls or lambda syntax.
  3. Final Answer:

    Runnable { println("Running") } -> Option A
  4. Quick Check:

    Runnable lambda = Runnable { ... } [OK]
Hint: Use Runnable { } directly for SAM conversion [OK]
Common Mistakes:
  • Trying to call run() method explicitly in lambda
  • Using arrow syntax incorrectly inside Runnable
  • Confusing method reference with lambda syntax
3.

What will be the output of this Kotlin code using SAM conversion with Comparator?

val list = listOf("apple", "banana", "cherry")
val sorted = list.sortedWith(Comparator { a, b -> b.length - a.length })
println(sorted)
medium
A. ["cherry", "banana", "apple"]
B. ["apple", "banana", "cherry"]
C. ["banana", "apple", "cherry"]
D. ["banana", "cherry", "apple"]

Solution

  1. Step 1: Understand the Comparator lambda

    The lambda compares strings by length descending: b.length - a.length.
  2. Step 2: Sort the list by string length descending

    "banana" (6), "apple" (5), "cherry" (6). Since "banana" and "cherry" have same length, their order depends on original order.
  3. Step 3: Determine sorted order

    Between "banana" and "cherry", "banana" comes first in original list, so it stays first. Then "cherry", then "apple" last.
  4. Final Answer:

    ["banana", "apple", "cherry"] -> Option C
  5. Quick Check:

    Sorted by length descending = [banana, apple, cherry] [OK]
Hint: Comparator lambda: b.length - a.length sorts descending [OK]
Common Mistakes:
  • Assuming ascending order instead of descending
  • Mixing up a and b in comparator lambda
  • Ignoring stable sort behavior for equal lengths
4.

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

  1. Step 1: Check if ActionListener is a Java interface with one method

    ActionListener is a Java interface with a single method actionPerformed(ActionEvent).
  2. Step 2: Verify SAM conversion applicability

    SAM conversion applies only to Java interfaces with exactly one abstract method, so it should work here.
  3. 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.
  4. Final Answer:

    SAM conversion works only if the interface is a Java interface with one method -> Option A
  5. 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

  1. Step 1: Confirm Task is a Java interface with one method

    Task has one method execute(int times), so SAM conversion applies.
  2. Step 2: Write correct lambda syntax for SAM conversion

    The lambda must accept one parameter (times) matching the method signature: Task { times -> ... }
  3. Step 3: Check each option

    Task { times -> println("Executed $times times") } correctly uses Task { times -> println(...) }. Others misuse method calls or omit parameter.
  4. Final Answer:

    Task { times -> println("Executed $times times") } -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard