0
0
Android Kotlinmobile~20 mins

Extension functions in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extension Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Identify the correct extension function syntax
Which option correctly defines an extension function isEven for Int that returns true if the number is even?
Aextension fun Int.isEven(): Boolean { return this % 2 == 0 }
Bfun Int.isEven: Boolean = this % 2 == 0
Cfun isEven(this Int): Boolean = this % 2 == 0
Dfun Int.isEven(): Boolean = this % 2 == 0
Attempts:
2 left
💡 Hint
Remember, extension functions use the syntax: fun ReceiverType.functionName() { ... }
ui_behavior
intermediate
2:00remaining
Extension function effect on UI component
Given this extension function for TextView in Android Kotlin:
fun TextView.makeRed() { this.setTextColor(android.graphics.Color.RED) }

What will happen when you call myTextView.makeRed() in an activity?
AThe text color of myTextView changes to red
BThe background color of myTextView changes to red
CThe app crashes with a NullPointerException
DNothing happens because extension functions cannot modify UI
Attempts:
2 left
💡 Hint
Extension functions can call existing methods on the receiver object.
lifecycle
advanced
2:00remaining
Extension function and lifecycle impact
Consider an extension function on Activity that shows a toast message:
fun Activity.showWelcome() { Toast.makeText(this, "Welcome!", Toast.LENGTH_SHORT).show() }

What is a potential lifecycle-related issue when calling showWelcome() inside onCreate() of an activity?
AThe toast may not show if the activity is not fully created yet
BThe toast will cause a memory leak if called in onCreate
CThe extension function will crash because Toast requires a Fragment
DThere is no issue; the toast always shows correctly
Attempts:
2 left
💡 Hint
Consider when UI elements are ready during the activity lifecycle.
navigation
advanced
2:00remaining
Extension function for navigation
You have this extension function for Fragment to navigate to another fragment:
fun Fragment.goToDetails() { findNavController().navigate(R.id.detailsFragment) }

What happens if you call goToDetails() in a fragment that is not attached to a NavController?
ANavigates to detailsFragment successfully
BCauses a compile-time error
CThrows an IllegalStateException at runtime
DDoes nothing silently
Attempts:
2 left
💡 Hint
findNavController() requires the fragment to be attached to a NavHost.
🔧 Debug
expert
2:00remaining
Debugging extension function unexpected behavior
Given this extension function:
fun String.removeFirstChar(): String = this.substring(1)

What happens if you call "".removeFirstChar() (empty string)?
AReturns an empty string
BThrows StringIndexOutOfBoundsException
CReturns null
DReturns the original empty string
Attempts:
2 left
💡 Hint
Check what substring(1) does on an empty string.