Bird
Raised Fist0
Kotlinprogramming~20 mins

Parameters with default values in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
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
intermediate
2: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")
A
Hello, Guest!
Hello, Alice!
Hello, Guest!
B
Hi, Guest!
Hello, Alice!
Hi, Guest!
C
Hello, Guest!
Hi, Alice!
Hi, Guest!
D
Hello, Guest!
Hello, Alice!
Hi, Guest!
Attempts:
2 left
💡 Hint
Remember that default parameters are used when no argument is provided for that parameter.
❓ Predict Output
intermediate
2: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))
A
110.0
88.0
120.0
B
110.0
80.0
100.0
C
110.0
80.0
120.0
D
110.0
88.0
100.0
Attempts:
2 left
💡 Hint
Check which parameters are passed explicitly and which use default values.
🔧 Debug
advanced
2: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)
APrints "Hello" three times
BRuntime error: times is null
CCompilation error: Non-default parameter 'times' cannot follow default parameter 'message'
DCompilation error: Missing argument for parameter 'times'
Attempts:
2 left
💡 Hint
In Kotlin, parameters with default values must be declared after parameters without default values.
❓ Predict Output
advanced
2: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")
A
5 red circle
5 square red
10 blue circle
B
5 red circle
5 red square
10 blue circle
C
5 red circle
square 5 red
10 blue circle
D
5 red circle
5 red square
5 blue circle
Attempts:
2 left
💡 Hint
Named arguments let you skip parameters and specify only some values.
🧠 Conceptual
expert
3: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()
AHi, Guest
BRuntime error: No default argument found
CCompilation error: Default parameter missing in override
DHello, Guest
Attempts:
2 left
💡 Hint
Default parameter values are resolved at compile time based on the reference type.

Practice

(1/5)
1. What happens when you call a Kotlin function with a parameter that has a default value but you don't provide an argument for it?
fun greet(name: String = "Guest") { println("Hello, $name!") }
easy
A. The function uses the default value for the missing parameter.
B. The function throws an error because the argument is missing.
C. The function ignores the parameter and prints nothing.
D. The function requires you to always provide the argument.

Solution

  1. Step 1: Understand default parameter behavior

    In Kotlin, if a parameter has a default value, the function can be called without providing that argument.
  2. Step 2: Apply to the example function

    Calling greet() will use the default value "Guest" for name, printing "Hello, Guest!".
  3. Final Answer:

    The function uses the default value for the missing parameter. -> Option A
  4. Quick Check:

    Default parameter means optional argument [OK]
Hint: If argument missing, default value is used automatically [OK]
Common Mistakes:
  • Thinking missing argument causes error
  • Assuming function ignores missing parameters
  • Believing all parameters must be provided
2. Which of the following is the correct way to declare a Kotlin function with a default parameter value?
easy
A. fun add(a: Int, b: Int default 5) { return a + b }
B. fun add(a: Int = 5, b: Int) { return a + b }
C. fun add(a: Int, b: Int) = a + b = 5
D. fun add(a: Int, b: Int = 5) { return a + b }

Solution

  1. Step 1: Check Kotlin syntax for default parameters

    In Kotlin, default values are assigned using = after the parameter type, like b: Int = 5.
  2. Step 2: Validate each option

    fun add(a: Int, b: Int = 5) { return a + b } correctly uses b: Int = 5. fun add(a: Int = 5, b: Int) { return a + b } places default before non-default parameter, which is allowed but can cause ambiguity if called without named arguments. Options A and C have invalid syntax.
  3. Final Answer:

    fun add(a: Int, b: Int = 5) { return a + b } -> Option D
  4. Quick Check:

    Default value syntax = parameter: Type = value [OK]
Hint: Use '=' after type to set default value [OK]
Common Mistakes:
  • Placing default value before type
  • Using 'default' keyword instead of '='
  • Assigning default outside parameter list
3. What is the output of the following Kotlin code?
fun multiply(x: Int, y: Int = 2) = x * y

println(multiply(4))
medium
A. 8
B. 4
C. Error: Missing argument for y
D. 2

Solution

  1. Step 1: Identify default parameter usage

    The function multiply has a default value of 2 for y. Calling multiply(4) means x=4 and y=2.
  2. Step 2: Calculate the result

    Multiply 4 by 2 to get 8.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    Default y=2 used, 4*2=8 [OK]
Hint: If argument missing, default value is used [OK]
Common Mistakes:
  • Expecting error for missing argument
  • Using only first argument as output
  • Confusing default value with zero
4. Identify the error in this Kotlin function declaration:
fun greet(name: String = "User", age: Int) {
    println("Hello, $name! You are $age years old.")
}
medium
A. Cannot use default values with String parameters.
B. Default parameter must be last or followed by named arguments.
C. Function must return a value if default parameters are used.
D. No error, function is correct.

Solution

  1. Step 1: Check parameter order rules

    In Kotlin, parameters with default values should be placed after parameters without default values, or you must use named arguments when calling.
  2. Step 2: Analyze given function

    Here, name has a default but comes before age which has no default. This causes a problem when calling without named arguments.
  3. Final Answer:

    Default parameter must be last or followed by named arguments. -> Option B
  4. Quick Check:

    Default parameters after non-default or use named args [OK]
Hint: Place default parameters after non-default ones [OK]
Common Mistakes:
  • Putting default parameters before required ones
  • Assuming all parameters can have defaults anywhere
  • Ignoring named argument calls
5. You want to write a Kotlin function formatMessage that takes a message and an optional prefix with default value "Info:". It should return the combined string. Which function correctly implements this and allows calling with or without the prefix?
hard
A. fun formatMessage(prefix: String = "Info:", message: String) = "$prefix $message"
B. fun formatMessage(message: String, prefix: String) = "$prefix $message"
C. fun formatMessage(message: String, prefix: String = "Info:") = "$prefix $message"
D. fun formatMessage(message: String = "Info:", prefix: String) = "$prefix $message"

Solution

  1. Step 1: Define function with default parameter last

    The optional parameter prefix with default value should come after the required message parameter.
  2. Step 2: Check each option

    fun formatMessage(message: String, prefix: String = "Info:") = "$prefix $message" correctly places prefix last with default. fun formatMessage(prefix: String = "Info:", message: String) = "$prefix $message" places default before required, which can cause call issues. Options C and D lack default for prefix or have wrong order.
  3. Final Answer:

    fun formatMessage(message: String, prefix: String = "Info:") = "$prefix $message" -> Option C
  4. Quick Check:

    Default last parameter enables optional argument [OK]
Hint: Put default parameters after required ones for easy calls [OK]
Common Mistakes:
  • Placing default parameter before required parameter
  • Not providing default value for optional parameter
  • Confusing parameter order causing call errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard