Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an inline function in Kotlin.
Kotlin
inline fun [1](block: () -> Unit) {
block()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a keyword as a function name.
Leaving the function name blank.
✗ Incorrect
The function name can be anything, but here 'inlineFun' is used as a clear example of an inline function name.
2fill in blank
mediumComplete the code to call the inline function with a lambda expression.
Kotlin
inlineFun {
println([1])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an unquoted string causing a syntax error.
Passing a function name instead of a string.
✗ Incorrect
The println function expects a string argument, so the string must be in quotes.
3fill in blank
hardFix the error in the inline function declaration by completing the code.
Kotlin
inline fun [1](crossinline block: () -> Unit) {
val runnable = Runnable {
block()
}
runnable.run()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different function names causing unresolved reference errors.
Omitting the 'inline' keyword.
✗ Incorrect
The function name must match the call site; here 'inlineFun' is the correct name to fix the error.
4fill in blank
hardFill both blanks to create a higher-order inline function that accepts a lambda and returns its result.
Kotlin
inline fun <T> [1](block: () -> T): T { return block[2]() }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '()' directly after 'block' without dot notation.
Choosing a function name that is a Kotlin keyword.
✗ Incorrect
The function name 'inlineCall' is chosen, and the lambda is invoked using '.invoke()' syntax.
5fill in blank
hardFill all three blanks to create an inline function with a reified type parameter and a lambda that returns a Boolean.
Kotlin
inline fun <reified [1]> [2](predicate: ([3]) -> Boolean): Boolean { return predicate.invoke(null as [1]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the type parameter and lambda parameter.
Omitting 'reified' keyword.
✗ Incorrect
The reified type parameter is 'T', the function name is 'checkType', and the lambda parameter type is 'T'.