Functions & Lambdas Questions with Answers

19 practice questions on Functions & Lambdas (Kotlin) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

fun area(w: Int, h: Int) = w * h
println(area(h = 2, w = 5))
  1. 7
  2. 25
  3. 10
  4. Compile error

Answer: C. 10

Explanation

Named arguments can be given in any order; w=5 and h=2 give 10.

Step-by-step solution

  1. w = 5, h = 2.
  2. Multiply.
  3. Result 10.

Why the other options are wrong

  • A. Area multiplies, it does not add.
  • B. h is 2, not 5.
  • D. Named arguments may come in any order.

Question 2Easy

What is the output?

val sq = { x: Int -> x * x }
println(sq(4))
  1. 16
  2. 8
  3. 4
  4. Compile error

Answer: A. 16

Explanation

The lambda multiplies its parameter by itself, 4 * 4 = 16.

Step-by-step solution

  1. sq is a function (Int) -> Int.
  2. sq(4) = 4*4.
  3. 16.

Why the other options are wrong

  • B. x * x is squaring, not doubling.
  • C. The lambda squares its argument.
  • D. Calling a lambda with () is valid.

Question 3Easy

What is the output?

fun greet(name: String = "World") = "Hello, $name"
println(greet())
  1. Hello, name
  2. Hello,
  3. Hello, World
  4. Compile error

Answer: C. Hello, World

Explanation

A default parameter value is used when the argument is omitted, and $name interpolates it.

Step-by-step solution

  1. The function greet has a default parameter 'World' for 'name'.
  2. greet() calls the function without an argument, so 'name' uses default value 'World'.
  3. $name interpolates the value of 'name', which is 'World'.
  4. So the output becomes 'Hello, World'.
  5. The correct answer is Hello, World.

Why the other options are wrong

  • A. $name is an interpolation, so the parameter value World is inserted, not the word name.
  • B. The string has the full text Hello, followed by the default value World.
  • D. A default parameter is valid Kotlin, so this code compiles and prints a greeting.

Tip: Remember default parameters and string interpolation in Kotlin.

Question 4Easy

What is the output?

fun greet(name: String, greeting: String = "Hi") = "$greeting $name"
println(greet("Ana"))
  1. Hi Ana
  2. Ana
  3. Hi
  4. Compile error

Answer: A. Hi Ana

Explanation

greeting falls back to its default "Hi", so the result is "Hi Ana".

Step-by-step solution

  1. Only name is passed.
  2. greeting uses default "Hi".
  3. Template gives "Hi Ana".

Why the other options are wrong

  • B. The default for greeting is still applied.
  • C. The name is part of the string.
  • D. Defaults let you omit trailing arguments.

Question 5Medium

In list.map { it * 2 }, what is it?

  1. A reference to the list itself that map is called on
  2. A reserved keyword for the receiver of the call
  3. The implicit name of a lambda's single parameter
  4. The index of the current element in the list

Answer: C. The implicit name of a lambda's single parameter

Explanation

When a lambda has exactly one parameter, Kotlin lets you refer to it as it.

Step-by-step solution

  1. In Kotlin, it is used to refer to the single parameter of a lambda.
  2. list.map { it * 2 } applies a function to each element in the list.
  3. The it here represents each element as the lambda is called on them.
  4. This makes it the implicit name for the lambda's single parameter.
  5. So, it refers to the current element being processed in the list.
  6. Final answer: The implicit name of a lambda's single parameter.

Why the other options are wrong

  • A. it is the lambda parameter, which holds each element in turn, not the list itself.
  • B. it is not a keyword; it is only an implicit name for a single lambda parameter.
  • D. map does not pass an index; use withIndex or mapIndexed to get indexes.

Tip: Remember: In Kotlin, it is the default parameter name for single-argument lambdas.

Question 6Medium

What is the output?

listOf(1, 2, 3).forEach {
    if (it == 2) return@forEach
    print(it)
}
  1. 13
  2. 1
  3. 123
  4. Compile error

Answer: A. 13

Explanation

return@forEach exits only the lambda for the current element, like continue.

Step-by-step solution

  1. it=1 prints.
  2. it=2 returns from lambda.
  3. it=3 prints.

Why the other options are wrong

  • B. return@forEach only skips this iteration.
  • C. 2 is skipped.
  • D. Labelled return from a lambda is valid.

Question 7Medium

What is the output?

fun f(a: Int, b: Int = a * 2) = a + b
println(f(3))
  1. 9
  2. 6
  3. 3
  4. Compile error

Answer: A. 9

Explanation

Defaults can refer to earlier parameters: b = 6, so a + b = 9.

Step-by-step solution

  1. b = 3*2 = 6.
  2. 3 + 6.
  3. 9.

Why the other options are wrong

  • B. b defaults to 6, so the sum is 9.
  • C. b is added too.
  • D. Defaults may use earlier parameters.

Question 8Medium

Why is list.filter { it > 2 } written without parentheses?

  1. filter is a keyword
  2. When the last parameter is a lambda it can be placed outside (or replace) the parentheses
  3. Kotlin ignores parentheses
  4. it is a reserved call operator

Answer: B. When the last parameter is a lambda it can be placed outside (or replace) the parentheses

Explanation

Trailing lambda syntax lets a final function-type argument go after the call parentheses, and the parentheses may be dropped if empty.

Step-by-step solution

  1. filter takes one lambda parameter.
  2. It is the last parameter.
  3. So braces alone are enough.

Why the other options are wrong

  • A. filter is an ordinary function.
  • C. They are required in other cases.
  • D. it is just the implicit parameter name.

Question 9Hard

What is the output?

fun f(vararg s: String) = s.joinToString("-")
println(f(*arrayOf("a", "b"), "c"))
  1. [a, b]-c
  2. abc
  3. Compile error
  4. a-b-c

Answer: D. a-b-c

Explanation

The spread operator * passes array elements as separate vararg arguments, then "c" follows.

Step-by-step solution

  1. * unpacks the array.
  2. Arguments: a, b, c.
  3. Joined with "-".

Why the other options are wrong

  • A. The spread operator * unpacks the array into separate arguments.
  • B. joinToString uses the given separator.
  • C. Mixing spread and normal arguments is allowed.

Question 10Hard

What does the inline modifier on a higher-order function primarily do?

  1. Copies the function body and lambda into call sites, avoiding lambda allocation
  2. Makes the function run on a background thread pool automatically
  3. Makes the function execute at compile time only, never at runtime
  4. Prevents the function from being overridden in any subclass

Answer: A. Copies the function body and lambda into call sites, avoiding lambda allocation

Explanation

Inlining removes the overhead of creating function objects for lambdas and also permits non-local returns from them.

Step-by-step solution

  1. Inline modifier copies lambda body into call sites, avoiding object creation.
  2. This reduces memory usage by eliminating lambda allocation overhead.
  3. It allows direct execution of the lambda code at call site.
  4. No function object is created for the lambda expression.
  5. This improves performance by reducing indirection.
  6. Final answer: Copies the function body and lambda into call sites, avoiding lambda allocation.

Why the other options are wrong

  • B. inline does not add threads; the function body still runs on the calling thread.
  • C. inline copies code into call sites; the function still runs at runtime.
  • D. Override control comes from open and final; inline does not change overriding.

Tip: Use inline for lambdas to avoid object creation and improve efficiency.

Practise all 19 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Kotlin topics · All topics