Bird
0
0

Identify the error in this Kotlin single-expression function:

medium📝 Debug Q14 of 15
Kotlin - Functions
Identify the error in this Kotlin single-expression function:
fun greet(name: String) = {
    "Hello, $name!"
}
AMissing return type declaration.
BString interpolation is invalid.
CUsing braces {} after equals sign is incorrect.
DFunction must have no parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Check single-expression function syntax

    Single-expression functions use '=' followed by an expression without braces.
  2. Step 2: Identify the error

    The braces after '=' create a lambda block, which is not allowed here; it should be just the expression.
  3. Final Answer:

    Using braces {} after equals sign is incorrect. -> Option C
  4. Quick Check:

    No braces allowed after '=' in single-expression functions [OK]
Quick Trick: No braces after '=' in single-expression functions [OK]
Common Mistakes:
MISTAKES
  • Adding braces after '='
  • Thinking return type is mandatory
  • Misusing string interpolation syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes