0
0
Android Kotlinmobile~10 mins

Passing arguments in Android 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 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'
Atext
Bmessage
Cinput
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than 'name' causes an error because 'name' is used inside the function.
2fill in blank
medium

Complete 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'
A"Alice"
Bname
CAlice
D'Alice'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing Alice without quotes causes a variable not found error.
Using single quotes is for characters, not strings.
3fill in blank
hard

Fix 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'
A123
B"Bob"
Ctrue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a number or boolean instead of a string causes a type mismatch error.
4fill in blank
hard

Fill 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'
Aname
Bage
Ccount
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that do not match the variables used inside the function causes errors.
5fill in blank
hard

Fill 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'
Aname
Bage
C25
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Not providing a default value for the second parameter causes errors when calling with one argument.