Challenge - 5 Problems
Extension Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediateIdentify the correct extension function syntax
Which option correctly defines an extension function
isEven for Int that returns true if the number is even?Attempts:
2 left
💡 Hint
Remember, extension functions use the syntax: fun ReceiverType.functionName() { ... }
✗ Incorrect
Option D correctly uses the Kotlin extension function syntax with the receiver type before the function name and a proper function body returning a Boolean.
❓ ui_behavior
intermediateExtension function effect on UI component
Given this extension function for
What will happen when you call
TextView in Android Kotlin:fun TextView.makeRed() { this.setTextColor(android.graphics.Color.RED) }What will happen when you call
myTextView.makeRed() in an activity?Attempts:
2 left
💡 Hint
Extension functions can call existing methods on the receiver object.
✗ Incorrect
The extension function calls setTextColor on the TextView instance, changing its text color to red.
❓ lifecycle
advancedExtension function and lifecycle impact
Consider an extension function on
What is a potential lifecycle-related issue when calling
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?Attempts:
2 left
💡 Hint
Consider when UI elements are ready during the activity lifecycle.
✗ Incorrect
If called too early, the UI might not be ready to display the toast properly, so the toast might not appear as expected.
advanced
Extension function for navigation
You have this extension function for
What happens if you call
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?Attempts:
2 left
💡 Hint
findNavController() requires the fragment to be attached to a NavHost.
✗ Incorrect
If the fragment is not attached to a NavController, findNavController() throws an IllegalStateException at runtime.
🔧 Debug
expertDebugging extension function unexpected behavior
Given this extension function:
What happens if you call
fun String.removeFirstChar(): String = this.substring(1)
What happens if you call
"".removeFirstChar() (empty string)?Attempts:
2 left
💡 Hint
Check what substring(1) does on an empty string.
✗ Incorrect
Calling substring(1) on an empty string causes StringIndexOutOfBoundsException because index 1 is out of range.
