0
0
Kotlinprogramming~10 mins

Unit type as void equivalent in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function that returns Unit.

Kotlin
fun greet(): [1] {
    println("Hello!")
}
Drag options to blanks, or click blank then click option'
AUnit
Bvoid
CNothing
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' instead of 'Unit' in Kotlin.
Using 'Nothing' which means the function never returns.
2fill in blank
medium

Complete the code to define a function with an explicit Unit return type.

Kotlin
fun printMessage(message: String): [1] {
    println(message)
}
Drag options to blanks, or click blank then click option'
AUnit
BInt
CString
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type like Int or String when the function does not return anything.
Omitting the return type and expecting it to be inferred.
3fill in blank
hard

Fix the error in the function declaration to correctly use the Unit type.

Kotlin
fun showInfo(): [1] {
    println("Info displayed")
}
Drag options to blanks, or click blank then click option'
Avoid
BNothing
CUnit
DAny
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' which is not valid in Kotlin.
Using 'Nothing' which means the function never returns.
4fill in blank
hard

Fill both blanks to create a function that returns Unit and prints a message.

Kotlin
fun display(): [1] {
    println([2])
}
Drag options to blanks, or click blank then click option'
AUnit
B"Hello, Kotlin!"
C"Welcome!"
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type other than Unit.
Printing the wrong message string.
5fill in blank
hard

Fill all three blanks to define a function that returns Unit, takes a String parameter, and prints it.

Kotlin
fun showMessage(msg: String): [1] {
    println([2])
    return [3]
}
Drag options to blanks, or click blank then click option'
AUnit
Bmsg
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning null instead of Unit.
Not printing the parameter correctly.