0
0
Kotlinprogramming~20 mins

Function declaration syntax in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple function call
What is the output of this Kotlin code?
Kotlin
fun greet(name: String): String {
    return "Hello, $name!"
}

fun main() {
    println(greet("Alice"))
}
AHello, Alice!
Bgreet(Alice)
CHello, name!
DError: Missing return statement
Attempts:
2 left
💡 Hint
Look at how the function returns a greeting string using the input name.
Predict Output
intermediate
2:00remaining
Function with default parameter value
What will this Kotlin program print?
Kotlin
fun multiply(x: Int, y: Int = 2): Int {
    return x * y
}

fun main() {
    println(multiply(5))
}
A10
B7
CError: Missing argument for y
D25
Attempts:
2 left
💡 Hint
Check the default value of parameter y.
Predict Output
advanced
2:00remaining
Single-expression function output
What does this Kotlin code print?
Kotlin
fun square(n: Int) = n * n

fun main() {
    println(square(4))
}
An * n
B4
CError: Missing return type
D16
Attempts:
2 left
💡 Hint
Single-expression functions return the expression value automatically.
Predict Output
advanced
2:00remaining
Function with no return value
What is the output of this Kotlin program?
Kotlin
fun printMessage(message: String): Unit {
    println("Message: $message")
}

fun main() {
    printMessage("Hi there")
}
AError: Function must return a value
BUnit
CMessage: Hi there
DHi there
Attempts:
2 left
💡 Hint
Unit means the function returns no meaningful value but can print output.
Predict Output
expert
2:00remaining
Function with named arguments and default values
What will this Kotlin code print?
Kotlin
fun formatText(text: String, prefix: String = "", suffix: String = "") = "$prefix$text$suffix"

fun main() {
    println(formatText(suffix = "!", text = "Hello"))
}
A!Hello
BHello!
CHello
DError: Named arguments must be in order
Attempts:
2 left
💡 Hint
Named arguments can be passed in any order in Kotlin.