Challenge - 5 Problems
Named Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function call with named arguments
What is the output of this Kotlin code using named arguments?
Kotlin
fun greet(name: String, greeting: String = "Hello") { println("$greeting, $name!") } greet(greeting = "Hi", name = "Anna")
Attempts:
2 left
💡 Hint
Look at how the arguments are passed by name, not by position.
✗ Incorrect
Named arguments allow you to specify parameters in any order. Here, greeting is "Hi" and name is "Anna", so the output is "Hi, Anna!".
❓ Predict Output
intermediate2:00remaining
Output with default and named arguments
What will this Kotlin code print?
Kotlin
fun calculate(x: Int, y: Int = 10, z: Int = 5) = x + y * z println(calculate(z = 3, x = 2))
Attempts:
2 left
💡 Hint
Remember the order of operations and which arguments are used.
✗ Incorrect
Named arguments allow passing z=3 and x=2, y uses default 10. So calculation is 2 + 10 * 3 = 2 + 30 = 32.
🔧 Debug
advanced2:00remaining
Identify the error with named arguments
What error does this Kotlin code produce?
Kotlin
fun displayInfo(name: String, age: Int) { println("Name: $name, Age: $age") } displayInfo(age = 25)
Attempts:
2 left
💡 Hint
Check if all required parameters have values.
✗ Incorrect
The function requires both 'name' and 'age'. Only 'age' is provided, so Kotlin reports missing 'name'.
📝 Syntax
advanced2:00remaining
Which call is syntactically correct?
Given the function below, which call is syntactically correct in Kotlin?
Kotlin
fun orderPizza(size: String, cheese: Boolean = true, pepperoni: Boolean = false) {} // Calls:
Attempts:
2 left
💡 Hint
Named arguments must come after positional arguments.
✗ Incorrect
In Kotlin, positional arguments must come before named arguments. Both A (all named) and C (positional first, then named) are correct. B and D have positional arguments after named arguments.
🚀 Application
expert3:00remaining
Determine the final map after function calls with named arguments
Consider this Kotlin function that updates a map. What is the final content of the map after these calls?
Kotlin
fun updateMap(map: MutableMap<String, Int>, key: String, value: Int = 1) { map[key] = map.getOrDefault(key, 0) + value } val myMap = mutableMapOf("a" to 2) updateMap(map = myMap, key = "b") updateMap(key = "a", map = myMap, value = 3) updateMap(myMap, "b", 4) println(myMap)
Attempts:
2 left
💡 Hint
Trace each call and how the map updates with default and named arguments.
✗ Incorrect
Initially, map has {a=2}. First call adds b=1 (default). Second adds 3 to a (2+3=5). Third adds 4 to b (1+4=5). Final map is {a=5, b=5}.