Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Data Types
What is the output of this Kotlin code?
fun doNothing(): Unit = Unit

fun main() {
    val x = doNothing()
    println(x == Unit)
}
ACompilation error
Bfalse
CRuntime exception
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Understand function return and compare returned value with Unit

    doNothing returns the singleton Unit object explicitly. Since x is Unit, x == Unit is true.
  2. Final Answer:

    true -> Option D
  3. Quick Check:

    Unit equality check returns true [OK]
Quick Trick: Unit is singleton, equality check returns true [OK]
Common Mistakes:
MISTAKES
  • Expecting false for Unit equality
  • Thinking Unit is a class instance
  • Confusing Unit with null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes