Challenge - 5 Problems
Unit Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin function returning Unit?
Consider the following Kotlin code. What will be printed when
printResult() is called?Kotlin
fun printResult(): Unit { println("Hello from Unit function") } fun main() { printResult() }
Attempts:
2 left
💡 Hint
Remember that Unit functions can print output but do not return a value to print.
✗ Incorrect
The function prints the string inside it. Since it returns Unit, the return value is not printed, only the println output appears.
❓ Predict Output
intermediate2:00remaining
What is the value of variable x after this Unit function call?
Look at this Kotlin code. What is the value of
x after calling val x = printMessage()?Kotlin
fun printMessage(): Unit { println("Message printed") } fun main() { val x = printMessage() println(x) }
Attempts:
2 left
💡 Hint
Functions returning Unit assign Unit to variables.
✗ Incorrect
In Kotlin, functions returning Unit assign the singleton Unit object to variables. So x is Unit, which prints as "kotlin.Unit".
🔧 Debug
advanced2:00remaining
Why does this Kotlin code cause a compilation error?
Examine the code below. Why does it fail to compile?
Kotlin
fun doSomething(): Unit { return 5 } fun main() { doSomething() }
Attempts:
2 left
💡 Hint
Unit means no meaningful value is returned.
✗ Incorrect
Functions declared with return type Unit cannot return any value. Returning 5 causes a type mismatch error.
🧠 Conceptual
advanced2:00remaining
Which statement about Kotlin's Unit type is true?
Choose the correct statement about the Unit type in Kotlin.
Attempts:
2 left
💡 Hint
Think about what Unit represents in Kotlin functions.
✗ Incorrect
Unit is a singleton object used to indicate a function returns no meaningful value, similar to void in Java but it is an actual type.
❓ Predict Output
expert2:00remaining
What is the output of this Kotlin code involving Unit?
Analyze the code and select the exact output printed to the console.
Kotlin
fun foo(): Unit { println("Start") return Unit } fun main() { val result = foo() println(result) }
Attempts:
2 left
💡 Hint
The function prints "Start" and returns Unit which prints as kotlin.Unit.
✗ Incorrect
The function prints "Start" first. The variable result holds Unit, which prints as "kotlin.Unit".