Bird
0
0

Identify the error in this Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Functions
Identify the error in this Kotlin code:
val greet: String -> String = { name -> "Hi, $name" }
println(greet("Bob"))
ACannot assign lambda to val
BMissing lambda arrow inside braces
CMissing parentheses in println
DIncorrect function type declaration syntax
Step-by-Step Solution
Solution:
  1. Step 1: Check function type declaration

    The correct syntax uses parentheses: (String) -> String, not String -> String.
  2. Step 2: Confirm other parts are correct

    Lambda syntax and println call are correct; only type declaration is wrong.
  3. Final Answer:

    Incorrect function type declaration syntax -> Option D
  4. Quick Check:

    Function type needs parentheses [OK]
Quick Trick: Function types need parentheses: (Type) -> ReturnType [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses in function type
  • Confusing lambda arrow with function type arrow
  • Thinking lambdas cannot be assigned to vals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes