MCQs on Advanced Functions | Kotlin

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

  1. 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
  2. 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
  3. Which keyword is required to define a higher-order function in Kotlin?
    a) fun
    b) func
    c) higher
    d) None of the above
  4. What is the correct syntax to call a higher-order function?
    a) higherOrderFunction(arg1, ::functionName)
    b) callFunction(functionName())
    c) useFunction { function }
    d) apply { function }
  5. Which of the following is NOT an example of a higher-order function in Kotlin?
    a) map
    b) filter
    c) println
    d) fold
  6. 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
  7. Which Kotlin collection function transforms each element of a list using a higher-order function?
    a) filter
    b) map
    c) reduce
    d) all
  8. 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

  1. 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
  2. How are lambda expressions declared in Kotlin?
    a) { parameters -> body }
    b) lambda(parameters) { body }
    c) func() { parameters, body }
    d) (parameters) => { body }
  3. What is the default name for the single parameter in a lambda expression?
    a) param
    b) arg
    c) it
    d) value
  4. 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 }
  5. 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.
  6. How do you invoke a lambda expression?
    a) lambdaName.invoke()
    b) lambdaName()
    c) call lambdaName()
    d) Both a and b
  7. 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
  8. 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.
  9. 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
  10. 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

  1. 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
  2. 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
  3. How do you declare an inline function in Kotlin?
    a) inline fun functionName()
    b) inline function functionName()
    c) fun inline functionName()
    d) functionName inline()
  4. 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.
  5. 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.
  6. Which modifier works with inline functions to restrict crossinline behavior?
    a) noinline
    b) restrict
    c) readonly
    d) safeinline
  7. 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.
  8. Which keyword ensures a lambda cannot exit its enclosing inline function?
    a) noexit
    b) crossinline
    c) returninline
    d) lambdaexit
  9. 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
  10. 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
  11. Which of the following functions are inline in Kotlin?
    a) run
    b) let
    c) apply
    d) All of the above
  12. 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

QNoAnswer (Option with text)
1a) A function that takes another function as an argument or returns a function
2b) By using a lambda expression
3a) fun
4a) higherOrderFunction(arg1, ::functionName)
5c) println
6c) Represents the implicit name of a single parameter
7b) map
8b) (ParameterTypes) -> ReturnType
9a) An anonymous function
10a) { parameters -> body }
11c) it
12a) val lambda = { x: Int -> x * x }
13b) Anonymous functions allow explicit return
14d) Both a and b
15c) Both a and b
16a) It modifies the receiver object
17a) A lambda declared at the end of a function call
18a) Yes, they have access to variables in their enclosing scope
19a) A function that compiles directly into the calling code
20c) Both a and b

Here are the answers for the last 10 questions:

QNoAnswer (Option with text)
21a) inline fun functionName()
22b) Its bytecode replaces the function call
23a) No, only higher-order functions can be inlined
24a) noinline
25a) It prevents inlining of specific lambda arguments
26b) crossinline
27b) Increased code size due to inlining
28c) Higher-order functions with small lambdas
29d) All of the above
30a) Yes, but only tail-recursive ones

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top