Bird
Raised Fist0
Kotlinprogramming~20 mins

Why lambdas enable functional style in Kotlin - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin lambda expression?
Consider the following Kotlin code using a lambda. What will it print?
Kotlin
val numbers = listOf(1, 2, 3, 4)
val doubled = numbers.map { it * 2 }
println(doubled)
A[1, 4, 9, 16]
B[1, 2, 3, 4]
C[2, 4, 6, 8]
DCompilation error
Attempts:
2 left
💡 Hint
The map function applies the lambda to each element.
🧠 Conceptual
intermediate
2:00remaining
Why do lambdas support functional programming in Kotlin?
Which statement best explains why lambdas enable a functional style in Kotlin?
ALambdas enforce mutable state for variables.
BLambdas automatically optimize code for faster execution.
CLambdas replace all loops with recursion.
DLambdas allow functions to be treated as values, enabling passing and returning functions.
Attempts:
2 left
💡 Hint
Think about how functions can be used in functional programming.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code with lambda cause?
What error will this Kotlin code produce when run? val list = listOf(1, 2, 3) val result = list.filter { it > 1 } .map { it.toString() } .reduce { acc, s -> acc + s } println(result)
Kotlin
val list = listOf(1, 2, 3)
val result = list.filter { it > 1 }
    .map { it.toString() }
    .reduce { acc, s -> acc + s }
println(result)
ACompilation error: reduce requires non-empty list
BNo error, prints "23"
CRuntimeException: UnsupportedOperationException
DNo error, prints "223"
Attempts:
2 left
💡 Hint
Check what filter and map produce before reduce.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using lambdas and sequences?
What will this Kotlin code print? val seq = sequenceOf(1, 2, 3, 4) val result = seq.filter { it % 2 == 0 } .map { it * it } .toList() println(result)
Kotlin
val seq = sequenceOf(1, 2, 3, 4)
val result = seq.filter { it % 2 == 0 }
    .map { it * it }
    .toList()
println(result)
A[4, 16]
B[1, 4, 9, 16]
C[2, 4]
DCompilation error: sequence operations not allowed
Attempts:
2 left
💡 Hint
Filter keeps even numbers, map squares them.
🚀 Application
expert
2:00remaining
How many items are in the resulting list after this Kotlin lambda chain?
Given this Kotlin code, how many items are in the final list? val data = listOf("apple", "banana", "cherry", "date") val filtered = data.filter { it.length > 5 } val mapped = filtered.map { it.uppercase() } val result = mapped.drop(1) println(result.size)
Kotlin
val data = listOf("apple", "banana", "cherry", "date")
val filtered = data.filter { it.length > 5 }
val mapped = filtered.map { it.uppercase() }
val result = mapped.drop(1)
println(result.size)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count items with length > 5, then drop the first.

Practice

(1/5)
1. Why do lambdas enable a functional programming style in Kotlin?
easy
A. Because they automatically optimize code for speed
B. Because they replace all loops with recursion
C. Because they require explicit variable declarations
D. Because they allow passing functions as arguments to other functions

Solution

  1. Step 1: Understand what lambdas do

    Lambdas are small functions that can be passed around like data.
  2. Step 2: Connect lambdas to functional style

    Functional programming relies on passing behavior (functions) as arguments, which lambdas enable.
  3. Final Answer:

    Because they allow passing functions as arguments to other functions -> Option D
  4. Quick Check:

    Lambdas enable passing behavior = C [OK]
Hint: Lambdas let you pass functions like data [OK]
Common Mistakes:
  • Thinking lambdas only replace loops
  • Believing lambdas optimize speed automatically
  • Confusing lambdas with variable declarations
2. Which of the following is the correct syntax for a lambda expression in Kotlin?
easy
A. val sum = { a: Int, b: Int -> a + b }
B. val sum = (a: Int, b: Int) => a + b
C. val sum = fun(a: Int, b: Int) { return a + b }
D. val sum = lambda(a: Int, b: Int) -> a + b

Solution

  1. Step 1: Recall Kotlin lambda syntax

    Kotlin lambdas use curly braces with parameters before the arrow, like { a, b -> ... }.
  2. Step 2: Compare options

    val sum = { a: Int, b: Int -> a + b } matches Kotlin syntax. Options B, C, and D use syntax from other languages or incorrect forms.
  3. Final Answer:

    val sum = { a: Int, b: Int -> a + b } -> Option A
  4. Quick Check:

    Kotlin lambdas use { params -> body } = A [OK]
Hint: Kotlin lambdas use curly braces and arrow -> [OK]
Common Mistakes:
  • Using => instead of -> in Kotlin
  • Writing full function syntax inside lambda
  • Using 'lambda' keyword which Kotlin doesn't have
3. What is the output of this Kotlin code?
val numbers = listOf(1, 2, 3, 4)
val doubled = numbers.map { it * 2 }
println(doubled)
medium
A. Compilation error
B. [1, 2, 3, 4]
C. [2, 4, 6, 8]
D. [3, 6, 9, 12]

Solution

  1. Step 1: Understand the map function with lambda

    map applies the lambda to each element, here multiplying each by 2.
  2. Step 2: Calculate the transformed list

    1*2=2, 2*2=4, 3*2=6, 4*2=8, so the list becomes [2, 4, 6, 8].
  3. Final Answer:

    [2, 4, 6, 8] -> Option C
  4. Quick Check:

    map doubles each element = [2, 4, 6, 8] [OK]
Hint: map applies lambda to each list item [OK]
Common Mistakes:
  • Thinking map modifies original list
  • Confusing map with filter
  • Expecting syntax error due to lambda
4. Identify the error in this Kotlin code using lambdas:
val list = listOf(1, 2, 3)
val filtered = list.filter { it > 2 }
val result = filtered.map { it * 2 }
println(result)
medium
A. Syntax error in lambda expression
B. No error; code runs and prints [6]
C. filter should be replaced with map
D. map cannot be used after filter

Solution

  1. Step 1: Analyze filter and map usage

    filter keeps elements greater than 2, so only 3 remains.
  2. Step 2: Apply map to filtered list

    map multiplies 3 by 2, resulting in [6].
  3. Final Answer:

    No error; code runs and prints [6] -> Option B
  4. Quick Check:

    filter then map works fine = A [OK]
Hint: filter then map is valid chain [OK]
Common Mistakes:
  • Assuming syntax error in lambdas
  • Thinking map can't follow filter
  • Confusing filter and map roles
5. Given a list of names, how can lambdas help you create a map of name lengths only for names longer than 3 characters?
Choose the correct Kotlin code snippet.
hard
A. val result = names.filter { it.length > 3 }.associate { it to it.length }
B. val result = names.map { it.length > 3 }.associateWith { it.length }
C. val result = names.associate { if (it.length > 3) it to it.length }
D. val result = names.map { it.length > 3 }.associate { it to it.length }

Solution

  1. Step 1: Filter names longer than 3 characters

    Use filter { it.length > 3 } to keep only long names.
  2. Step 2: Create a map of name to length

    Use associate { it to it.length } to map each name to its length.
  3. Final Answer:

    val result = names.filter { it.length > 3 }.associate { it to it.length } -> Option A
  4. Quick Check:

    filter then associate with pairs = B [OK]
Hint: Filter first, then associate with pairs [OK]
Common Mistakes:
  • Using associateWith incorrectly
  • Trying to map booleans instead of names
  • Returning null in associate causing errors
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