Bird
Raised Fist0
Kotlinprogramming~10 mins

Lambda syntax and declaration in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a lambda that adds two integers.

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 }
Cfun(a: Int, b: Int) = a + b
Dlambda(a, b) { a + b }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for lambda declaration.
Trying to use 'fun' keyword inside lambda assignment.
Using incorrect lambda syntax like 'lambda(...)'.
2fill in blank
medium

Complete the code to declare a lambda that returns the length of a string.

Kotlin
val length: (String) -> Int = [1]
Drag options to blanks, or click blank then click option'
Afun(s: String) = s.length
B(s) -> s.length
C{ s -> s.length }
Dlambda(s) { s.length }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces.
Trying to declare lambda with 'fun' keyword.
Using incorrect lambda syntax like 'lambda(...)'.
3fill in blank
hard

Fix the error in the lambda declaration that multiplies two numbers.

Kotlin
val multiply: (Int, Int) -> Int = [1]
Drag options to blanks, or click blank then click option'
A(a, b) -> a * b
Blambda(a, b) { a * b }
Cfun(a: Int, b: Int) = a * b
D{ a, b -> a * b }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for lambda.
Trying to use 'fun' keyword inside lambda assignment.
Using 'lambda' keyword which is not valid in Kotlin.
4fill in blank
hard

Fill both blanks to declare a lambda that checks if a number is even.

Kotlin
val isEven: (Int) -> Boolean = { num [1] num [2] 2 == 0 }
Drag options to blanks, or click blank then click option'
A%
B==
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '+' instead of '%' for remainder.
Using '=' instead of '==' for comparison.
5fill in blank
hard

Fill all three blanks to create a lambda that filters a list of strings by length greater than 3.

Kotlin
val filtered = words.filter { [1] -> [2].length [3] 3 }
Drag options to blanks, or click blank then click option'
Aword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for comparison.
Using different parameter names inconsistently.

Practice

(1/5)
1. What is a lambda expression in Kotlin?
easy
A. A small unnamed function stored in a variable
B. A class declaration
C. A type of loop
D. A way to declare variables

Solution

  1. Step 1: Understand lambda basics

    Lambdas are small functions without a name that can be stored in variables.
  2. Step 2: Compare with other options

    Classes, loops, and variable declarations are different concepts from lambdas.
  3. Final Answer:

    A small unnamed function stored in a variable -> Option A
  4. Quick Check:

    Lambda = unnamed function [OK]
Hint: Lambdas are unnamed functions in curly braces [OK]
Common Mistakes:
  • Confusing lambdas with classes
  • Thinking lambdas are loops
  • Mixing lambdas with variable declarations
2. Which of the following is the correct syntax for a lambda that takes two Ints and returns their sum?
easy
A. { a, b : Int -> a + b }
B. (a, b) -> { a + b }
C. fun(a, b) { return a + b }
D. { a, b -> a + b }

Solution

  1. Step 1: Recall lambda syntax

    Lambdas use curly braces with parameters before an arrow, then the body: { params -> body }
  2. Step 2: Check each option

    { a, b -> a + b } matches correct syntax. (a, b) -> { a + b } wrongly uses parentheses outside braces. fun(a, b) { return a + b } is a function, not a lambda. { a, b : Int -> a + b } incorrectly places type annotation.
  3. Final Answer:

    { a, b -> a + b } -> Option D
  4. Quick Check:

    Correct lambda syntax = { params -> body } [OK]
Hint: Lambdas use braces with arrow separating params and body [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Trying to write full function syntax
  • Incorrect type annotations inside lambda
3. What is the output of this Kotlin code?
val multiply = { x: Int, y: Int -> x * y }
println(multiply(3, 4))
medium
A. 12
B. 7
C. 34
D. Error

Solution

  1. Step 1: Understand the lambda function

    The lambda takes two Ints x and y, and returns their product x * y.
  2. Step 2: Calculate the output

    Calling multiply(3, 4) returns 3 * 4 = 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply parameters inside lambda to get output [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating numbers as strings
  • Syntax errors in lambda call
4. Identify the error in this Kotlin lambda declaration:
val greet = { name String -> "Hello, $name" }
medium
A. Lambda body must be outside braces
B. Arrow is missing after parameters
C. Missing colon between parameter and type
D. Cannot use string interpolation in lambda

Solution

  1. Step 1: Check parameter syntax

    Parameters must have a colon between name and type: name: String
  2. Step 2: Verify lambda structure

    The arrow -> is present after parameters, and string interpolation is allowed inside lambdas.
  3. Final Answer:

    Missing colon between parameter and type -> Option C
  4. Quick Check:

    Parameter syntax = name: Type [OK]
Hint: Use colon between param name and type in lambdas [OK]
Common Mistakes:
  • Omitting colon in parameter declaration
  • Forgetting arrow after parameters
  • Thinking string interpolation is disallowed
5. How can you declare a lambda in Kotlin that filters a list of integers to keep only even numbers?
hard
A. val evenFilter = { list: List<Int> -> list.filter(it % 2 == 0) }
B. val evenFilter = { list: List<Int> -> list.filter { it % 2 == 0 } }
C. val evenFilter = { list: List<Int> -> list.filter { x -> x / 2 == 0 } }
D. val evenFilter = { list: List<Int> -> list.filter { x -> x % 2 = 0 } }

Solution

  1. Step 1: Understand filter usage

    filter takes a lambda returning true for elements to keep. Correct condition is x % 2 == 0 for even numbers.
  2. Step 2: Check each option

    val evenFilter = { list: List<Int> -> list.filter { it % 2 == 0 } } correctly uses filter with lambda and modulus check. val evenFilter = { list: List<Int> -> list.filter(it % 2 == 0) } misses braces for lambda. val evenFilter = { list: List<Int> -> list.filter { x -> x / 2 == 0 } } uses division instead of modulus. val evenFilter = { list: List<Int> -> list.filter { x -> x % 2 = 0 } } uses assignment (=) instead of comparison (==).
  3. Final Answer:

    val evenFilter = { list: List<Int> -> list.filter { it % 2 == 0 } } -> Option B
  4. Quick Check:

    Filter with { it % 2 == 0 } keeps evens [OK]
Hint: Use filter { it % 2 == 0 } to select even numbers [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (==)
  • Using division (/) instead of modulus (%)
  • Omitting braces around lambda in filter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard