0
0
Android Kotlinmobile~5 mins

Functions and lambdas in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function in Kotlin?
A function is a reusable block of code that performs a specific task. It can take inputs, called parameters, and can return a result.
Click to reveal answer
beginner
How do you declare a simple function in Kotlin that adds two numbers?
fun add(a: Int, b: Int): Int { return a + b }
Click to reveal answer
intermediate
What is a lambda expression in Kotlin?
A lambda is a small anonymous function that can be passed as a value. It is often used for short pieces of code like callbacks or operations on collections.
Click to reveal answer
intermediate
How do you write a lambda that multiplies a number by 2?
val double = { x: Int -> x * 2 }
Click to reveal answer
intermediate
What is the difference between a named function and a lambda in Kotlin?
A named function has a name and can be called anywhere by that name. A lambda is anonymous and usually used as a value passed to other functions.
Click to reveal answer
How do you declare a function in Kotlin that returns no value?
Afun greet() { println("Hi") }
Bfun greet(): Int { println("Hi") }
Cfun greet() -> Unit { println("Hi") }
Dfunction greet() { println("Hi") }
What symbol separates parameters and the body in a lambda expression?
A:
B->
C=
D=>
Which of these is a valid lambda that takes no parameters and returns 5?
A{ -> 5 }
Bfun() = 5
C{ x -> 5 }
D{ 5 }
How do you call a function named 'sum' with arguments 3 and 4?
Asum(3, 4)
Bsum 3, 4
Ccall sum(3, 4)
Dsum.call(3,4)
What is the type of the lambda val square = { x: Int -> x * x }?
AUnit
BInt
C(Int) -> Int
DFunction
Explain how to declare and use a simple function in Kotlin with parameters and a return value.
Think about how you tell the app to do something with inputs and give back a result.
You got /5 concepts.
    Describe what a lambda expression is and give an example of when you might use one in an Android app.
    Imagine you want to quickly tell a button what to do when clicked without writing a full function.
    You got /5 concepts.