Bird
0
0

Find the error in this Kotlin function:

medium📝 Debug Q7 of 15
Kotlin - Functions
Find the error in this Kotlin function:
fun printMessage(message: String): Unit {
    println(message)
    return message
}
AFunction declared to return Unit but returns a String
BParameter type is missing
CMissing parentheses in function declaration
Dprintln usage is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Check declared return type

    The function declares return type Unit, which means no meaningful value is returned.
  2. Step 2: Analyze return statement

    The function tries to return message (a String), which conflicts with Unit return type.
  3. Final Answer:

    Function declared to return Unit but returns a String -> Option A
  4. Quick Check:

    Unit functions should not return values [OK]
Quick Trick: Unit functions should not return any value [OK]
Common Mistakes:
MISTAKES
  • Returning a value from Unit functions
  • Omitting parentheses in declaration
  • Incorrect println syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes