Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named greet that takes no parameters and returns a String.
Android Kotlin
fun greet(): [1] { return "Hello!" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Unit as return type when function returns a value.
Using Int or Boolean instead of String.
✗ Incorrect
The function greet returns a String, so the return type must be String.
2fill in blank
mediumComplete the code to define a lambda that takes two Ints and returns their sum.
Android Kotlin
val sum: (Int, Int) -> Int = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for lambda body.
Using subtraction or multiplication instead of addition.
✗ Incorrect
The lambda syntax uses curly braces with parameters and an arrow, here adding a and b.
3fill in blank
hardFix the error in the function declaration to make it a valid Kotlin function that returns Unit.
Android Kotlin
fun printMessage(message: String)[1] {
println(message)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-> Unit' which is invalid syntax for function return type.
Using String as return type when function returns nothing.
✗ Incorrect
In Kotlin, functions returning Unit use ': Unit' after the parameter list.
4fill in blank
hardFill both blanks to create a lambda that filters a list of Ints to only even numbers.
Android Kotlin
val evens = numbers.filter { [1] -> [2] % 2 == 0 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and inside the lambda body.
Using invalid variable names.
✗ Incorrect
The lambda parameter name must be consistent; here 'number' is used for both parameter and in the body.
5fill in blank
hardFill all three blanks to create a function that takes a lambda and calls it with 5.
Android Kotlin
fun callWithFive(action: (Int) -> Unit) {
action([1])
}
callWithFive { [2] -> println([3]) } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing wrong value to the lambda.
Using different names for lambda parameter and inside body.
✗ Incorrect
The function calls the lambda with 5; the lambda parameter is named x and printed.