Bird
0
0

Which of the following is the correct syntax to assign a lambda function to a variable in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Functions
Which of the following is the correct syntax to assign a lambda function to a variable in Kotlin?
Aval greet = fun() { println("Hello") }
Bval greet = function() println("Hello")
Cval greet = lambda { println("Hello") }
Dval greet = () -> println("Hello")
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin lambda syntax

    Kotlin lambda syntax uses arrow notation: () -> expression or block.
  2. Step 2: Compare options with correct syntax

    val greet = () -> println("Hello") matches the correct lambda syntax. val greet = fun() { println("Hello") } is a named function expression, B and D are invalid syntax.
  3. Final Answer:

    val greet = () -> println("Hello") -> Option D
  4. Quick Check:

    Lambda syntax = () -> expression [OK]
Quick Trick: Look for arrow notation () -> for lambdas [OK]
Common Mistakes:
MISTAKES
  • Using 'function()' keyword which doesn't exist
  • Confusing named function with lambda syntax
  • Using 'lambda' keyword which is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes