Bird
0
0

What will be the output of the following Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Data Types
What will be the output of the following Kotlin code?
fun printMessage(): Unit {
    println("Hello, Kotlin!")
}

fun main() {
    val result = printMessage()
    println(result)
}
AHello, Kotlin!\nkotlin.Unit
BHello, Kotlin!\nnull
CHello, Kotlin!\n0
DHello, Kotlin!\n(no output)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the function return type

    The function printMessage returns Unit, which is a singleton object in Kotlin representing no meaningful value.
  2. Step 2: Analyze the output of println(result)

    When printing result, which is Unit, Kotlin prints "kotlin.Unit" as its string representation.
  3. Final Answer:

    Hello, Kotlin!\nkotlin.Unit -> Option A
  4. Quick Check:

    Unit prints as 'kotlin.Unit' string [OK]
Quick Trick: Printing Unit shows 'kotlin.Unit' text [OK]
Common Mistakes:
MISTAKES
  • Expecting null instead of Unit
  • Expecting no output for Unit
  • Confusing Unit with zero or empty string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes