Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Functions
What will be the output of this Kotlin code?
fun greet(name: String): String = "Hi, $name!"

fun main() {
    println(greet("Anna"))
}
AHi, Anna!
BHello, Anna!
Cgreet("Anna")
DError: missing return type
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the greet function

    The function uses expression body syntax to return the string "Hi, $name!" with the name inserted.
  2. Step 2: Check the output of greet("Anna")

    It returns "Hi, Anna!", which is printed by println.
  3. Final Answer:

    Hi, Anna! -> Option A
  4. Quick Check:

    Expression body returns greeting string = Hi, Anna! [OK]
Quick Trick: Expression body functions return value directly with '=' [OK]
Common Mistakes:
MISTAKES
  • Confusing greeting text
  • Omitting return type in declaration
  • Printing function call instead of result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes