Challenge - 5 Problems
Master of Default Parameters in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with default parameters
What is the output of this Kotlin code?
Kotlin
fun greet(name: String = "Guest", greeting: String = "Hello") { println("$greeting, $name!") } greet() greet("Alice") greet(greeting = "Hi")
Attempts:
2 left
💡 Hint
Remember that default parameters are used when no argument is provided for that parameter.
✗ Incorrect
The function greet uses default values for both parameters. When called without arguments, it prints "Hello, Guest!". When called with "Alice", it uses the default greeting "Hello". When called with named argument greeting = "Hi", it uses default name "Guest".
❓ Predict Output
intermediate2:00remaining
Function call with mixed default and explicit parameters
What will this Kotlin code print?
Kotlin
fun calculate(price: Int, tax: Double = 0.1, discount: Double = 0.0): Double { return price * (1 + tax) * (1 - discount) } println(calculate(100)) println(calculate(100, discount = 0.2)) println(calculate(100, 0.2))
Attempts:
2 left
💡 Hint
Check which parameters are passed explicitly and which use default values.
✗ Incorrect
First call uses default tax 0.1 and discount 0.0: 100 * 1.1 * 1 = 110.0. Second call overrides discount to 0.2 but uses default tax 0.1: 100 * 1.1 * 0.8 = 88.0 (but option A says 80.0, so check carefully). Third call passes tax=0.2 and discount default 0.0: 100 * 1.2 * 1 = 120.0. So option A's second output is wrong, correct is 88.0. So option A is correct.
🔧 Debug
advanced2:00remaining
Identify the error in default parameter usage
This Kotlin function has a problem. What error will it cause?
Kotlin
fun printMessage(message: String = "Hello", times: Int) { repeat(times) { println(message) } } printMessage(times = 3)
Attempts:
2 left
💡 Hint
In Kotlin, parameters with default values must be declared after parameters without default values.
✗ Incorrect
Kotlin requires that parameters with default values come after parameters without default values. Here, 'message' has a default but comes before 'times' which has no default. This causes a compilation error.
❓ Predict Output
advanced2:00remaining
Output with multiple default parameters and named arguments
What is the output of this Kotlin code?
Kotlin
fun describe(color: String = "red", shape: String = "circle", size: Int = 5) { println("$size $color $shape") } describe() describe(shape = "square") describe(size = 10, color = "blue")
Attempts:
2 left
💡 Hint
Named arguments let you skip parameters and specify only some values.
✗ Incorrect
First call uses all defaults: 5 red circle. Second call overrides shape to square, others default: 5 red square. Third call overrides size and color, shape default: 10 blue circle.
🧠 Conceptual
expert3:00remaining
Behavior of default parameters with inheritance
Consider these Kotlin classes:
class Base {
open fun greet(name: String = "Guest") {
println("Hello, $name")
}
}
class Derived : Base() {
override fun greet(name: String) {
println("Hi, $name")
}
}
val obj: Base = Derived()
obj.greet()
Attempts:
2 left
💡 Hint
Default parameter values are resolved at compile time based on the reference type.
✗ Incorrect
In Kotlin, default parameter values are determined by the compile-time type. Here, obj is of type Base, so greet() uses Base's default parameter "Guest". The overridden greet in Derived does not have a default, but it is called with the default argument from Base, so it prints "Hi, Guest".