Bird
0
0

Identify the error in this Kotlin single-expression function:

medium📝 Debug Q6 of 15
Kotlin - Functions
Identify the error in this Kotlin single-expression function:
fun greet() = println("Hi") return "Done"
AMissing '=' sign
BCannot have a return statement after expression
CFunction must have braces for multiple statements
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze single-expression function rules

    Single-expression functions must have only one expression after '=' and cannot have additional statements.
  2. Step 2: Identify the problem

    The code has a return statement after the expression, which is invalid syntax.
  3. Final Answer:

    Cannot have a return statement after expression -> Option B
  4. Quick Check:

    Single-expression functions allow only one expression [OK]
Quick Trick: Single-expression functions cannot have multiple statements [OK]
Common Mistakes:
MISTAKES
  • Adding return after expression
  • Mixing block and single-expression syntax
  • Ignoring '=' sign

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes