Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Functions
What is the output of this Kotlin code?
val greet: (String) -> String = { name -> "Hello, $name!" }
println(greet("Anna"))
AHello, name!
BHello, Anna!
Cgreet(Anna)
DError: Missing function call
Step-by-Step Solution
Solution:
  1. Step 1: Understand the lambda function

    The lambda takes a String and returns "Hello, $name!" with the argument inserted.
  2. Step 2: Evaluate the println statement

    Calling greet("Anna") returns "Hello, Anna!" which is printed.
  3. Final Answer:

    Hello, Anna! -> Option B
  4. Quick Check:

    Lambda call output = Hello, Anna! [OK]
Quick Trick: String templates insert variable values with $ [OK]
Common Mistakes:
MISTAKES
  • Printing the variable name instead of its value
  • Forgetting to call the function with parentheses
  • Confusing the lambda parameter with a literal string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes