Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Functions
What will be the output of the following Kotlin code?
fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int {
    return op(x, y)
}

fun main() {
    val sum = operate(5, 3) { a, b -> a + b }
    println(sum)
}
A8
B53
CError: Missing function body
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand the operate function

    It takes two Ints and a function 'op' that combines them, then returns the result of op(x, y).
  2. Step 2: Analyze the lambda passed

    The lambda { a, b -> a + b } adds the two numbers 5 and 3, so operate returns 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    5 + 3 = 8 [OK]
Quick Trick: Trace lambda execution with given inputs [OK]
Common Mistakes:
MISTAKES
  • Concatenating numbers as strings instead of adding
  • Expecting syntax error due to lambda
  • Ignoring the lambda parameter and outputting 0

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes