Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around string default values.
Using a variable name instead of a value.
✗ Incorrect
In Kotlin, default parameter values must be provided as expressions. Here, "Guest" is a string literal used as the default value.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an empty string "" instead of no argument.
Passing null which is not allowed without nullable type.
✗ Incorrect
To use the default parameter value, you can call the function without passing any argument, so the blank should be empty.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "1" instead of integer 1.
Using null for a non-nullable parameter.
✗ Incorrect
The default value for an Int parameter must be an integer literal. 1 is the multiplicative identity, so it makes sense as a default.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up which default value goes to which parameter.
Forgetting quotes around string defaults.
✗ Incorrect
The first parameter text defaults to "Hello" and the prefix defaults to "* ". Both are string literals.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values for integer parameters.
Omitting quotes around the unit string.
✗ Incorrect
The length defaults to 10, width to 1, and unit to "cm²". Calling calculateArea(5) uses length=5, width=1, unit="cm²".