Challenge - 5 Problems
Kotlin Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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")) }
Attempts:
2 left
💡 Hint
Look at how the function returns a greeting string using the input name.
✗ Incorrect
The function greet takes a string parameter and returns a greeting message. Calling greet("Alice") returns "Hello, Alice!" which is printed.
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Check the default value of parameter y.
✗ Incorrect
The function multiply has a default value of 2 for y. Calling multiply(5) uses y=2, so 5*2=10 is printed.
❓ Predict Output
advanced2:00remaining
Single-expression function output
What does this Kotlin code print?
Kotlin
fun square(n: Int) = n * n fun main() { println(square(4)) }
Attempts:
2 left
💡 Hint
Single-expression functions return the expression value automatically.
✗ Incorrect
The function square returns n multiplied by n. For input 4, it returns 16.
❓ Predict Output
advanced2: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") }
Attempts:
2 left
💡 Hint
Unit means the function returns no meaningful value but can print output.
✗ Incorrect
The function prints the message with prefix "Message:". The output is that line.
❓ Predict Output
expert2: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")) }
Attempts:
2 left
💡 Hint
Named arguments can be passed in any order in Kotlin.
✗ Incorrect
The function concatenates prefix, text, and suffix. Here prefix is default empty, suffix is "!", text is "Hello", so output is "Hello!".