Bird
0
0

Identify the error in the following Kotlin code that uses a function as a parameter:

medium📝 Debug Q14 of 15
Kotlin - Functions
Identify the error in the following Kotlin code that uses a function as a parameter:
fun applyOperation(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
    return operation(x, y)
}

fun main() {
    val result = applyOperation(4, 2) { a, b -> a * b
    println(result)
}
AIncorrect function parameter types.
BMissing closing brace '}' for the lambda expression.
CapplyOperation should return Unit, not Int.
DLambda parameters must be declared outside the function call.
Step-by-Step Solution
Solution:
  1. Step 1: Check lambda syntax in main()

    The lambda passed to applyOperation lacks a closing brace '}', causing a syntax error.
  2. Step 2: Verify other parts

    Parameter types and return types are correct; lambda parameters inside call are valid.
  3. Final Answer:

    Missing closing brace '}' for the lambda expression. -> Option B
  4. Quick Check:

    Unclosed lambda = syntax error [OK]
Quick Trick: Count opening and closing braces in lambdas [OK]
Common Mistakes:
MISTAKES
  • Assuming parameter types are wrong
  • Thinking return type must be Unit
  • Believing lambda parameters can't be inline

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes