In Kotlin, the equivalent of Java's 'void' is the 'Unit' type. It represents a function that returns no meaningful value.
Click to reveal answer
beginner
What value does a Kotlin function with return type 'Unit' return?
A Kotlin function with return type 'Unit' returns a single instance called 'Unit'. It is similar to returning nothing but is an actual object.
Click to reveal answer
beginner
Can you omit the 'Unit' return type in Kotlin functions?
Yes, if a Kotlin function returns 'Unit', you can omit the return type declaration. The compiler infers it automatically.
Click to reveal answer
intermediate
How does Kotlin's 'Unit' type help in writing cleaner code compared to 'void'?
Since 'Unit' is a real type with a value, it can be used in generics and expressions, unlike 'void' which is just a keyword. This makes Kotlin code more consistent and expressive.
Click to reveal answer
beginner
Show a simple Kotlin function that returns 'Unit' explicitly.
fun greet(): Unit {
println("Hello!")
}
This function returns 'Unit' explicitly, meaning it returns no meaningful value but completes its action.
Click to reveal answer
What does the Kotlin 'Unit' type represent?
AA function that returns null
BA function that returns an integer
CA function that returns no meaningful value
DA function that throws an exception
✗ Incorrect
The 'Unit' type in Kotlin means the function returns no meaningful value, similar to 'void' in other languages.
Can you omit the 'Unit' return type in Kotlin function declarations?
AYes, the compiler infers it automatically
BOnly if the function is private
CNo, it must always be declared
DOnly if the function is inline
✗ Incorrect
Kotlin allows omitting the 'Unit' return type because the compiler infers it when no other return type is specified.
Which of these is a valid Kotlin function signature for a function returning 'Unit'?
Afun printMessage(): Int
Bfun printMessage(): Unit
Cfun printMessage(): String
Dfun printMessage(): null
✗ Incorrect
'fun printMessage(): Unit' is valid and means the function returns no meaningful value.
What is the value returned by a Kotlin function with return type 'Unit'?
Anull
BNo value, it throws an error
C0
DUnit instance
✗ Incorrect
Kotlin functions returning 'Unit' return a singleton instance of 'Unit', not null or zero.
Why is Kotlin's 'Unit' type more flexible than Java's 'void'?
ABecause 'Unit' is a real type and can be used in generics
BBecause 'Unit' can hold multiple values
CBecause 'Unit' is a keyword like 'void'
DBecause 'Unit' can be null
✗ Incorrect
'Unit' is a real type with a value, so it can be used in generics and expressions, unlike 'void' which is just a keyword.
Explain the role of the 'Unit' type in Kotlin and how it compares to 'void' in other languages.
Think about what happens when a function doesn't return anything.
You got /4 concepts.
Write a Kotlin function that prints a message and explicitly returns 'Unit'.
Use fun functionName(): Unit { ... }
You got /3 concepts.
Practice
(1/5)
1. What does the Unit type represent in Kotlin?
easy
A. A function that returns an integer
B. A function that returns no meaningful value
C. A function that returns a string
D. A function that throws an exception
Solution
Step 1: Understand the meaning of Unit
Unit in Kotlin means the function does not return any meaningful value, similar to void in other languages.
Step 2: Compare with other return types
Other options mention returning values like integer or string, which are not related to Unit.
Final Answer:
A function that returns no meaningful value -> Option B
Quick Check:
Unit = no meaningful return [OK]
Hint: Remember: Unit means no useful return value [OK]
Common Mistakes:
Confusing Unit with returning null
Thinking Unit is the same as returning an object
Assuming Unit means the function returns nothing at all
2. Which of the following is the correct way to declare a function that returns Unit explicitly in Kotlin?
easy
A. fun greet(): Unit { println("Hello") }
B. fun greet() -> Unit { println("Hello") }
C. fun greet(): void { println("Hello") }
D. fun greet() { return Unit }
Solution
Step 1: Check Kotlin function syntax for return type
Kotlin uses a colon : before the return type, so fun greet(): Unit is correct.
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.
Final Answer:
fun greet(): Unit { println("Hello") } -> Option A
Quick Check:
Return type declared with colon and Unit [OK]
Hint: Use colon before Unit, not arrow or void [OK]
Common Mistakes:
Using 'void' instead of 'Unit'
Using arrow '->' instead of colon ':'
Omitting return type but trying to return Unit explicitly
3. What will be the output of the following Kotlin code?
fun printMessage(): Unit {
println("Hello, Kotlin!")
}
fun main() {
val result = printMessage()
println(result)
}
medium
A. Hello, Kotlin!\nkotlin.Unit
B. Hello, Kotlin!\nnull
C. Hello, Kotlin!\n0
D. Hello, Kotlin!\n(no output)
Solution
Step 1: Understand the function return type
The function printMessage returns Unit, which is a singleton object in Kotlin representing no meaningful value.
Step 2: Analyze the output of println(result)
When printing result, which is Unit, Kotlin prints "kotlin.Unit" as its string representation.
Final Answer:
Hello, Kotlin!\nkotlin.Unit -> Option A
Quick Check:
Unit prints as 'kotlin.Unit' string [OK]
Hint: Printing Unit shows 'kotlin.Unit' text [OK]
Common Mistakes:
Expecting null instead of Unit
Expecting no output for Unit
Confusing Unit with zero or empty string
4. Identify the error in the following Kotlin function:
fun show(): Unit {
return
println("Hi")
}
medium
A. Function must not declare Unit return type explicitly
B. Missing return value for Unit
C. The println statement is unreachable after return
D. Function should return null instead of Unit
Solution
Step 1: Check the function flow
The return statement ends the function immediately, so the println("Hi") after it is unreachable code.
Step 2: Understand Kotlin rules about unreachable code
Kotlin does not allow unreachable code after a return statement, so this is a syntax error.
Final Answer:
The println statement is unreachable after return -> Option C
Quick Check:
Code after return is unreachable [OK]
Hint: Code after return is unreachable, watch statement order [OK]
Common Mistakes:
Thinking Unit requires explicit return value
Assuming explicit Unit return is forbidden
Confusing unreachable code with runtime error
5. You want to write a Kotlin function that logs a message and returns no value. Which of these is the best practice?
hard
A. Declare the function with return type void and omit return statement
B. Declare the function without specifying return type and use return Unit at the end
C. Declare the function with return type Unit and use return without value
D. Declare the function with return type Unit explicitly and omit the return statement
Solution
Step 1: Understand Kotlin's Unit usage
In Kotlin, Unit is the equivalent of void and can be omitted because it is the default return type for functions without a meaningful return.
Step 2: Evaluate best practice
Explicitly declaring Unit return type and omitting the return statement is clear and idiomatic. Using return Unit or return without value is allowed but less common. void is invalid in Kotlin.
Final Answer:
Declare the function with return type Unit explicitly and omit the return statement -> Option D
Quick Check:
Explicit Unit return type without return statement [OK]
Hint: Explicit Unit return type is clear and idiomatic [OK]
Common Mistakes:
Using 'void' which is invalid in Kotlin
Returning Unit explicitly when not needed
Omitting return type but returning Unit explicitly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.