Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function that takes one argument named name.
Android Kotlin
fun greet([1]: String) { println("Hello, $name!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than 'name' causes an error because 'name' is used inside the function.
✗ Incorrect
The function parameter must be named 'name' to match the usage inside the function.
2fill in blank
mediumComplete the code to call the function greet with the argument "Alice".
Android Kotlin
greet([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Alice without quotes causes a variable not found error.
Using single quotes is for characters, not strings.
✗ Incorrect
String arguments in Kotlin must be enclosed in double quotes.
3fill in blank
hardFix the error in the function call by passing the correct argument.
Android Kotlin
fun greet(name: String) {
println("Hello, $name!")
}
greet([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a number or boolean instead of a string causes a type mismatch error.
✗ Incorrect
The function expects a String argument, so we must pass a string like "Bob".
4fill in blank
hardFill both blanks to define a function with two parameters and call it correctly.
Android Kotlin
fun greet([1]: String, [2]: Int) { println("Hello, $name! You are $age years old.") } greet("Alice", 30)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that do not match the variables used inside the function causes errors.
✗ Incorrect
The parameters must be named 'name' and 'age' to match the print statement and function call.
5fill in blank
hardFill all three blanks to create a function with default arguments and call it with one argument.
Android Kotlin
fun greet([1]: String, [2]: Int = [3]) { println("Hello, $name! You are $age years old.") } greet("Charlie")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not providing a default value for the second parameter causes errors when calling with one argument.
✗ Incorrect
The function has parameters 'name' and 'age' with a default value 25 for age, so calling greet("Charlie") works.