0
0
Kotlinprogramming~10 mins

SAM conversions for Java interfaces in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Runnable using SAM conversion.

Kotlin
val runnable = Runnable { println([1]) }
runnable.run()
Drag options to blanks, or click blank then click option'
Aprintln("Hello from SAM")
B"Hello from SAM"
CHello from SAM
Drun()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Trying to call println inside the lambda incorrectly.
2fill in blank
medium

Complete the code to create a Comparator using SAM conversion that compares integers.

Kotlin
val comparator = Comparator<Int> { a, b -> a [1] b }
println(comparator.compare(5, 3))
Drag options to blanks, or click blank then click option'
A/
B+
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Using multiplication or division which do not give correct comparison.
3fill in blank
hard

Fix the error in the SAM conversion for ActionListener.

Kotlin
val listener = ActionListener { e -> println(e.[1]) }
Drag options to blanks, or click blank then click option'
AactionCommand
BgetActionCommand()
CgetAction()
Dcommand
Attempts:
3 left
💡 Hint
Common Mistakes
Using the Java getter method syntax instead of Kotlin property.
Using incorrect method names.
4fill in blank
hard

Fill both blanks to create a Thread using SAM conversion and start it.

Kotlin
val thread = Thread([1])
thread.[2]()
Drag options to blanks, or click blank then click option'
A{ println("Running in thread") }
Bstart
Crun
DThread()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling run() instead of start() on the thread.
Not providing a lambda as Runnable.
5fill in blank
hard

Fill all three blanks to create a Timer with delay 1000, an ActionListener using SAM conversion, and start the timer.

Kotlin
val timer = Timer([1], [2])
timer.[3]()
Drag options to blanks, or click blank then click option'
A1000
B{ e -> println("Timer fired") }
Cstart
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using stop() instead of start() to run the timer.
Not providing the correct delay value.