Bird
0
0

Which of the following is the correct way to declare a function that returns Unit explicitly in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Data Types
Which of the following is the correct way to declare a function that returns Unit explicitly in Kotlin?
Afun greet(): Unit { println("Hello") }
Bfun greet() -> Unit { println("Hello") }
Cfun greet(): void { println("Hello") }
Dfun greet() { return Unit }
Step-by-Step Solution
Solution:
  1. Step 1: Check Kotlin function syntax for return type

    Kotlin uses a colon : before the return type, so fun greet(): Unit is correct.
  2. Step 2: Validate each option

    fun greet() -> Unit { println("Hello") } uses an arrow -> which is invalid syntax. fun greet(): void { println("Hello") } uses void which is not a Kotlin type. fun greet() { return Unit } tries to return Unit explicitly but misses the return type declaration.
  3. Final Answer:

    fun greet(): Unit { println("Hello") } -> Option A
  4. Quick Check:

    Return type declared with colon and Unit [OK]
Quick Trick: Use colon before Unit, not arrow or void [OK]
Common Mistakes:
MISTAKES
  • Using 'void' instead of 'Unit'
  • Using arrow '->' instead of colon ':'
  • Omitting return type but trying to return Unit explicitly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes