0
0
Kotlinprogramming~10 mins

Parameters with default values 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 define a function with a default parameter value.

Kotlin
fun greet(name: String = [1]) {
    println("Hello, $name!")
}
Drag options to blanks, or click blank then click option'
A"Guest"
BGuest
C"name"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around string default values.
Using a variable name instead of a value.
2fill in blank
medium

Complete the function call to use the default parameter value.

Kotlin
fun greet(name: String = "Guest") {
    println("Hello, $name!")
}

greet([1])
Drag options to blanks, or click blank then click option'
A(no argument)
B""
C"John"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an empty string "" instead of no argument.
Passing null which is not allowed without nullable type.
3fill in blank
hard

Fix the error in the function definition by adding a default value to the parameter.

Kotlin
fun multiply(a: Int, b: Int = [1]): Int {
    return a * b
}
Drag options to blanks, or click blank then click option'
A0
B1
C"1"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "1" instead of integer 1.
Using null for a non-nullable parameter.
4fill in blank
hard

Fill both blanks to create a function with two parameters, each having default values.

Kotlin
fun formatText(text: String = [1], prefix: String = [2]) {
    println("$prefix$text")
}
Drag options to blanks, or click blank then click option'
A"Hello"
B"World"
C"* "
D"- "
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up which default value goes to which parameter.
Forgetting quotes around string defaults.
5fill in blank
hard

Fill all three blanks to define a function with default values and call it with one argument.

Kotlin
fun calculateArea(length: Int = [1], width: Int = [2], unit: String = [3]): String {
    val area = length * width
    return "$area $unit"
}

println(calculateArea(5))
Drag options to blanks, or click blank then click option'
A10
B1
C"cm²"
D"m²"
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values for integer parameters.
Omitting quotes around the unit string.