Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define an extension function isEven for Int that returns true if the number is even.
Android Kotlin
fun Int.[1](): Boolean = this % 2 == 0
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not describe the purpose clearly.
Forgetting to declare the function as an extension on Int.
✗ Incorrect
The extension function name should be
isEven to clearly indicate it checks if the number is even.2fill in blank
mediumComplete the code to call the extension function isEven on the integer number.
Android Kotlin
val number = 10 val result = number.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the function without parentheses.
Using a wrong function name.
✗ Incorrect
You call the extension function by using the dot notation on the variable:
number.isEven().3fill in blank
hardFix the error in the extension function that tries to add a greet function to String but is missing the receiver type.
Android Kotlin
fun [1].greet() = "Hello, $this!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the receiver type causes a syntax error.
Placing the receiver type after the function name.
✗ Incorrect
Extension functions must specify the receiver type before the function name, like
fun String.greet().4fill in blank
hardFill both blanks to create an extension function repeatText for String that repeats the string n times.
Android Kotlin
fun String.[1](n: Int): String = this.[2](n)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using operators like * or + directly on strings without the repeat function.
Choosing a function name that does not match the task.
✗ Incorrect
The function name is
repeatText. To repeat a string in Kotlin, use the repeat(n) function, but since this is an extension, we call this.repeat(n).5fill in blank
hardFill all three blanks to create an extension function toTitleCase for String that capitalizes the first letter and makes the rest lowercase.
Android Kotlin
fun String.[1](): String = this.substring(0,1).[2]() + this.substring(1).[3]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
capitalize() which capitalizes the first letter but leaves the rest unchanged.Not converting the rest of the string to lowercase.
✗ Incorrect
The function name is
toTitleCase. Use substring(0,1).uppercase() to capitalize the first character and lowercase() on the rest with substring(1).