Dive deep into Kotlin’s advanced functional programming features, including higher-order functions, lambda expressions, and inline functions. These tools unlock powerful, efficient, and expressive code for modern development.
Higher-Order Functions
What is a higher-order function in Kotlin? a) A function that takes another function as an argument or returns a function b) A function defined inside another function c) A function with more than three parameters d) A function that uses coroutines
How do you pass a function as an argument in Kotlin? a) By wrapping it in an object b) By using a lambda expression c) By defining it within the main function d) By passing it as a string
Which keyword is required to define a higher-order function in Kotlin? a) fun b) func c) higher d) None of the above
What is the correct syntax to call a higher-order function? a) higherOrderFunction(arg1, ::functionName) b) callFunction(functionName()) c) useFunction { function } d) apply { function }
Which of the following is NOT an example of a higher-order function in Kotlin? a) map b) filter c) println d) fold
What is the role of it in higher-order functions? a) Represents the return type b) Refers to the receiver object c) Represents the implicit name of a single parameter d) Refers to the context of the main function
Which Kotlin collection function transforms each element of a list using a higher-order function? a) filter b) map c) reduce d) all
How do you specify a function type in Kotlin? a) Function(args) -> Return b) (ParameterTypes) -> ReturnType c) TypeOfFunction(args) -> result d) funType(args): Return
Lambda Expressions
What is a lambda expression in Kotlin? a) An anonymous function b) A higher-order function c) A named function with default arguments d) A function inside a class
How are lambda expressions declared in Kotlin? a) { parameters -> body } b) lambda(parameters) { body } c) func() { parameters, body } d) (parameters) => { body }
What is the default name for the single parameter in a lambda expression? a) param b) arg c) it d) value
How can a lambda expression be assigned to a variable? a) val lambda = { x: Int -> x * x } b) fun lambda = { x: Int -> x * x } c) val lambda: x -> x * x d) lambda var = { x: Int => x * x }
What is the difference between a lambda and an anonymous function? a) Lambdas cannot return values. b) Anonymous functions allow explicit return. c) Lambdas must be named. d) Anonymous functions are slower.
How do you invoke a lambda expression? a) lambdaName.invoke() b) lambdaName() c) call lambdaName() d) Both a and b
Which of the following is a valid use of a lambda expression? a) Passing it to a higher-order function b) Storing it in a variable c) Both a and b d) None of the above
What does the apply function do with lambdas? a) It modifies the receiver object. b) It executes the lambda and returns its result. c) It always returns Unit. d) It prevents null values in lambdas.
What is a trailing lambda in Kotlin? a) A lambda declared at the end of a function call b) A lambda with no parameters c) A lambda that must return null d) A lambda used for exception handling
Can a lambda expression in Kotlin access variables outside its scope? a) Yes, they have access to variables in their enclosing scope. b) No, lambdas are self-contained. c) Only if explicitly declared. d) Only inside companion objects.
Inline Functions
What is an inline function in Kotlin? a) A function that compiles directly into the calling code b) A function that is executed in parallel c) A function that accepts no parameters d) A function that only supports lambdas
Why are inline functions useful in Kotlin? a) To reduce runtime overhead of higher-order functions b) To make code execution faster c) Both a and b d) They are not useful
How do you declare an inline function in Kotlin? a) inline fun functionName() b) inline function functionName() c) fun inline functionName() d) functionName inline()
What happens when an inline function is compiled? a) It is converted to a lambda. b) Its bytecode replaces the function call. c) It is ignored by the compiler. d) It is executed in a separate thread.
Can a regular function be marked as inline in Kotlin? a) No, only higher-order functions can be inlined. b) Yes, any function can be marked inline. c) Only if it has a Unit return type. d) Only if it is recursive.
Which modifier works with inline functions to restrict crossinline behavior? a) noinline b) restrict c) readonly d) safeinline
How does noinline affect lambda parameters in inline functions? a) It prevents inlining of specific lambda arguments. b) It forces lambda inlining. c) It blocks all inline optimizations. d) It disables type inference.
Which keyword ensures a lambda cannot exit its enclosing inline function? a) noexit b) crossinline c) returninline d) lambdaexit
What is the main drawback of inline functions? a) Increased compile time b) Increased code size due to inlining c) Cannot use higher-order functions d) Limited support for recursion
Inline functions are best suited for: a) Recursive algorithms b) Functions with complex logic c) Higher-order functions with small lambdas d) Functions that throw exceptions
Which of the following functions are inline in Kotlin? a) run b) let c) apply d) All of the above
Can inline functions be recursive in Kotlin? a) Yes, but only tail-recursive ones. b) No, recursion is not allowed in inline functions. c) Only if they use lambda parameters. d) Only when marked as crossinline.
Answer Key
QNo
Answer (Option with text)
1
a) A function that takes another function as an argument or returns a function
2
b) By using a lambda expression
3
a) fun
4
a) higherOrderFunction(arg1, ::functionName)
5
c) println
6
c) Represents the implicit name of a single parameter
7
b) map
8
b) (ParameterTypes) -> ReturnType
9
a) An anonymous function
10
a) { parameters -> body }
11
c) it
12
a) val lambda = { x: Int -> x * x }
13
b) Anonymous functions allow explicit return
14
d) Both a and b
15
c) Both a and b
16
a) It modifies the receiver object
17
a) A lambda declared at the end of a function call
18
a) Yes, they have access to variables in their enclosing scope
19
a) A function that compiles directly into the calling code
20
c) Both a and b
Here are the answers for the last 10 questions:
QNo
Answer (Option with text)
21
a) inline fun functionName()
22
b) Its bytecode replaces the function call
23
a) No, only higher-order functions can be inlined
24
a) noinline
25
a) It prevents inlining of specific lambda arguments