0
0
Android Kotlinmobile~5 mins

Extension functions in Android Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an extension function in Kotlin?
An extension function lets you add a new function to an existing class without changing its source code. It helps you extend functionality easily.
Click to reveal answer
beginner
How do you declare an extension function in Kotlin?
You write the receiver type followed by a dot and the function name. For example: fun String.shout() = this.uppercase() + "!"
Click to reveal answer
intermediate
Can extension functions access private members of the class they extend?
No, extension functions cannot access private or protected members of the class. They work like regular functions with the receiver object as a parameter.
Click to reveal answer
intermediate
What happens if an extension function has the same signature as a member function?
The member function always takes precedence over the extension function when called on an instance.
Click to reveal answer
beginner
Why are extension functions useful in Android development?
They let you add helpful functions to Android classes like View or Context without subclassing, making code cleaner and easier to read.
Click to reveal answer
How do you call an extension function on a String named text?
Atext.extensionFunction()
BextensionFunction(text)
CString.extensionFunction(text)
Dcall extensionFunction on String
Which keyword is used to declare an extension function in Kotlin?
Aextend
Bfun
Coverride
Dnewfun
Can extension functions modify the state of the object they extend?
AYes, always
BNo, they cannot modify state
COnly if the object is mutable
DOnly if declared inside the class
What is the receiver in an extension function?
AThe parameter list
BThe function name
CThe return type
DThe class being extended
If a class has a member function and an extension function with the same name, which one is called?
AMember function
BIt causes a compile error
CBoth are called
DExtension function
Explain what an extension function is and how it can be declared in Kotlin.
Think about adding new functions to existing classes without changing them.
You got /3 concepts.
    Describe the rules about access and precedence when using extension functions in Kotlin.
    Consider what happens if a class already has a function with the same name.
    You got /3 concepts.