0
0
Kotlinprogramming~10 mins

Named arguments for clarity in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the function with named arguments for clarity.

Kotlin
fun greet(name: String, age: Int) {
    println("Hello, $name! You are $age years old.")
}

greet(name = "Alice", age = 30)
Drag options to blanks, or click blank then click option'
Aage
Bgreet
Cname
Dprintln
Attempts:
3 left
💡 Hint
Common Mistakes
Using the function name or other unrelated words inside the string.
2fill in blank
medium

Complete the function call using named arguments to improve readability.

Kotlin
fun calculateArea(length: Double, width: Double): Double {
    return length * width
}

val area = calculateArea([1] = 5.0, [2] = 3.0)
Drag options to blanks, or click blank then click option'
Aarea
Bwidth
Clength
DcalculateArea
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name or omitting names.
3fill in blank
hard

Fix the error in the function call by using named arguments correctly.

Kotlin
fun displayInfo(name: String, city: String) {
    println("$name lives in $city.")
}

displayInfo([1] = "Bob", city = "New York")
Drag options to blanks, or click blank then click option'
Aname
Bprintln
CdisplayInfo
Dcity
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the wrong parameter or mixing named and positional arguments incorrectly.
4fill in blank
hard

Fill both blanks to call the function with named arguments in any order.

Kotlin
fun orderCoffee(size: String, type: String) {
    println("Order: $size $type coffee")
}

orderCoffee([1] = "Latte", [2] = "Large")
Drag options to blanks, or click blank then click option'
Atype
Bsize
CorderCoffee
Dprintln
Attempts:
3 left
💡 Hint
Common Mistakes
Using positional arguments or wrong parameter names.
5fill in blank
hard

Fill all three blanks to define and call a function using named arguments for clarity.

Kotlin
fun [1](firstName: String, lastName: String, age: Int) {
    println("$firstName $lastName is $age years old.")
}

[1]([2] = "John", [3] = "Doe", age = 25)
Drag options to blanks, or click blank then click option'
AprintPerson
BfirstName
ClastName
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function or parameter names.