0
0
Android Kotlinmobile~10 mins

Extension functions 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 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'
AisEven
BevenCheck
CcheckEven
DisOdd
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.
2fill in blank
medium

Complete 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'
AcheckEven
BisEven
CisOdd
DevenCheck
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the function without parentheses.
Using a wrong function name.
3fill in blank
hard

Fix 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'
AString
Bthis
Cfun
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the receiver type causes a syntax error.
Placing the receiver type after the function name.
4fill in blank
hard

Fill 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'
ArepeatText
B*
C+
Drepeat
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.
5fill in blank
hard

Fill 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'
AtoTitleCase
Buppercase
Ccapitalize
Dlowercase
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.