Bird
0
0

Identify the error in the following Kotlin function:

medium📝 Debug Q14 of 15
Kotlin - Data Types
Identify the error in the following Kotlin function:
fun show(): Unit {
    return
    println("Hi")
}
AFunction must not declare Unit return type explicitly
BMissing return value for Unit
CThe <code>println</code> statement is unreachable after return
DFunction should return null instead of Unit
Step-by-Step Solution
Solution:
  1. Step 1: Check the function flow

    The return statement ends the function immediately, so the println("Hi") after it is unreachable code.
  2. Step 2: Understand Kotlin rules about unreachable code

    Kotlin does not allow unreachable code after a return statement, so this is a syntax error.
  3. Final Answer:

    The println statement is unreachable after return -> Option C
  4. Quick Check:

    Code after return is unreachable [OK]
Quick Trick: Code after return is unreachable, watch statement order [OK]
Common Mistakes:
MISTAKES
  • Thinking Unit requires explicit return value
  • Assuming explicit Unit return is forbidden
  • Confusing unreachable code with runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes