Bird
Raised Fist0
Kotlinprogramming~20 mins

Named arguments for clarity in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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")
AHi, Anna!
BHello, Anna!
CHi, greeting!
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the arguments are passed by name, not by position.
❓ Predict Output
intermediate
2: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))
ACompilation error
B17
C32
D15
Attempts:
2 left
💡 Hint
Remember the order of operations and which arguments are used.
🔧 Debug
advanced
2: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)
AError: No value passed for parameter 'name'
BCompilation succeeds with no output
CPrints: Name: , Age: 25
DError: Named argument 'age' not found
Attempts:
2 left
💡 Hint
Check if all required parameters have values.
📝 Syntax
advanced
2: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:
AorderPizza(size = "Medium", pepperoni = true)
BorderPizza(cheese = false, "Large")
CorderPizza("Small", pepperoni = true, cheese = false)
DorderPizza(pepperoni = true, cheese = false, "Large")
Attempts:
2 left
💡 Hint
Named arguments must come after positional arguments.
🚀 Application
expert
3: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)
A{a=2, b=8}
B{a=3, b=5}
C{a=5, b=1}
D{a=5, b=5}
Attempts:
2 left
💡 Hint
Trace each call and how the map updates with default and named arguments.

Practice

(1/5)
1. What is the main benefit of using named arguments in Kotlin functions?
easy
A. They make it clear which value is assigned to which parameter.
B. They automatically optimize the function's performance.
C. They allow functions to run without parameters.
D. They convert all arguments to strings.

Solution

  1. Step 1: Understand named arguments purpose

    Named arguments explicitly show which value goes to which parameter, improving code readability.
  2. Step 2: Compare with other options

    Options A, C, and D describe unrelated or incorrect effects of named arguments.
  3. Final Answer:

    They make it clear which value is assigned to which parameter. -> Option A
  4. Quick Check:

    Named arguments clarify parameter assignment = B [OK]
Hint: Named arguments clarify parameter names and values [OK]
Common Mistakes:
  • Thinking named arguments improve performance
  • Believing named arguments remove the need for parameters
  • Confusing named arguments with type conversion
2. Which of the following is the correct way to call the function fun greet(name: String, age: Int) using named arguments?
easy
A. greet(age = 25, "Anna")
B. greet(name = "Anna", 25)
C. greet(name = "Anna", age = 25)
D. greet(name: "Anna", age: 25)

Solution

  1. Step 1: Check named argument syntax

    Named arguments use parameter = value syntax, so greet(name = "Anna", age = 25) is correct.
  2. Step 2: Identify incorrect options

    greet(name = "Anna", 25) mixes positional and named incorrectly; C has wrong order without naming first argument; D uses colon instead of equals.
  3. Final Answer:

    greet(name = "Anna", age = 25) -> Option C
  4. Quick Check:

    Named argument syntax uses = not : = A [OK]
Hint: Use parameter = value format for named arguments [OK]
Common Mistakes:
  • Using colon (:) instead of equals (=) in named arguments
  • Mixing positional and named arguments incorrectly
  • Passing arguments in wrong order without naming
3. What will be the output of the following Kotlin code?
fun describePerson(name: String, age: Int = 30) {
    println("Name: $name, Age: $age")
}

describePerson(age = 22, name = "John")
medium
A. Name: John, Age: 30
B. Name: John, Age: 22
C. Name: 22, Age: John
D. Compilation error

Solution

  1. Step 1: Analyze named arguments order

    Named arguments allow changing order, so age = 22 and name = "John" correctly assign values.
  2. Step 2: Check default parameter usage

    Since age is provided, default 30 is ignored. Output prints name and age as given.
  3. Final Answer:

    Name: John, Age: 22 -> Option B
  4. Quick Check:

    Named arguments reorder parameters correctly = C [OK]
Hint: Named arguments let you reorder parameters safely [OK]
Common Mistakes:
  • Assuming order must match function definition
  • Ignoring default parameter when argument is provided
  • Expecting compilation error due to argument order
4. Identify the error in the following Kotlin function call:
fun calculate(x: Int, y: Int = 10) = x + y

calculate(y = 5, 3)
medium
A. Function call is correct
B. Missing argument for parameter x
C. Default parameter cannot be overridden
D. Cannot use named argument before positional argument

Solution

  1. Step 1: Check argument order rules

    In Kotlin, positional arguments must come before named arguments. Here, named argument y=5 comes before positional 3, which is invalid.
  2. Step 2: Confirm error type

    This causes a syntax error because named arguments cannot precede positional ones.
  3. Final Answer:

    Cannot use named argument before positional argument -> Option D
  4. Quick Check:

    Named arguments must follow positional arguments = D [OK]
Hint: Positional args first, then named args [OK]
Common Mistakes:
  • Placing named arguments before positional arguments
  • Assuming default parameters can't be overridden
  • Ignoring Kotlin's argument order rules
5. Given the function fun bookTicket(name: String, seat: String = "Standard", meal: String = "None"), which call correctly books a ticket for "Alice" with a "VIP" seat and default meal using named arguments?
hard
A. bookTicket(name = "Alice", seat = "VIP")
B. bookTicket("Alice", "VIP", meal = "None")
C. bookTicket(seat = "VIP", "Alice")
D. bookTicket(name = "Alice", meal = "None")

Solution

  1. Step 1: Identify parameters to set

    We want to set name = "Alice" and seat = "VIP", but keep meal default.
  2. Step 2: Check each option

    bookTicket(name = "Alice", seat = "VIP") sets name and seat with named arguments, skipping meal (default used). bookTicket("Alice", "VIP", meal = "None") mixes positional and named but explicitly sets meal to "None" (same as default). bookTicket(seat = "VIP", "Alice") uses named argument before positional argument, invalid. bookTicket(name = "Alice", meal = "None") sets meal explicitly but not seat.
  3. Step 3: Choose best clarity and correctness

    bookTicket(name = "Alice", seat = "VIP") clearly sets only needed parameters with named arguments, using default meal.
  4. Final Answer:

    bookTicket(name = "Alice", seat = "VIP") -> Option A
  5. Quick Check:

    Named args set needed params, skip defaults = A [OK]
Hint: Name only needed params, skip defaults with named args [OK]
Common Mistakes:
  • Setting default parameters explicitly when not needed
  • Mixing positional and named arguments unnecessarily
  • Changing argument order without naming
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