0
0
Android Kotlinmobile~10 mins

Functions and lambdas in Android 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 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'
ABoolean
BInt
CUnit
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using Unit as return type when function returns a value.
Using Int or Boolean instead of String.
2fill in blank
medium

Complete 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'
A{ a, b -> a * b }
B(a, b) -> a - b
C{ a, b -> a + b }
D(a, b) -> a / b
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for lambda body.
Using subtraction or multiplication instead of addition.
3fill in blank
hard

Fix 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'
A: Unit
B-> Unit
C-> String
D: String
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.
4fill in blank
hard

Fill 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'
Anum
Bnumber
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and inside the lambda body.
Using invalid variable names.
5fill in blank
hard

Fill 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'
A5
Bx
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Passing wrong value to the lambda.
Using different names for lambda parameter and inside body.