0
0
Kotlinprogramming~10 mins

Lambda syntax and declaration in Kotlin - Interactive Code Practice

Choose your learning style9 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.