Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a function named greet.
Kotlin
fun [1]() { println("Hello!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than requested.
Forgetting to write the function name after
fun.✗ Incorrect
The function name should be greet as specified.
2fill in blank
mediumComplete the code to declare a function that returns an Int value 5.
Kotlin
fun getNumber(): [1] { return 5 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
String or other types instead of Int.Omitting the return type when the function returns a value.
✗ Incorrect
The function returns an integer, so the return type should be Int.
3fill in blank
hardFix the error in the function declaration to accept a String parameter named name.
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 used inside the function.
Capitalizing the parameter name incorrectly.
✗ Incorrect
The parameter name should be name to match the usage inside the function.
4fill in blank
hardFill both blanks to declare a function that takes two Int parameters and returns their sum.
Kotlin
fun add([1]: Int, [2]: Int): Int { return a + b }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the return statement.
Swapping the order of parameters.
✗ Incorrect
The parameters should be named a and b to match the return statement.
5fill in blank
hardFill all three blanks to declare a function that returns a greeting message with the given name.
Kotlin
fun greet([1]: String): [2] { return "Hello, " + [3] }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter or return types.
Using a different variable name than the parameter.
✗ Incorrect
The function takes a parameter name of type String and returns a String greeting using name.