Extensions and Infix Functions
Extension functions and infix functions in Kotlin provide powerful ways to enhance existing classes and improve code readability. Here's a list of questions and interview questions related to extensions and infix functions, along with examples and explanations:
1. What is an extension function in Kotlin?
An extension function is a feature that allows you to add new functions to existing classes without modifying their source code.
2. How do you declare an extension function in Kotlin?
Kotlin
Copied
3. Can you extend final classes with extension functions in Kotlin?
No, extension functions cannot be added to final classes.
4. Explain the purpose of the this
keyword in an extension function.
The this
keyword refers to the instance of the class on which the extension function is called.
5. What are the visibility modifiers that can be applied to extension functions?
Extension functions can have the same visibility modifiers as regular functions (public
, protected
, internal
, or private
).
6. How do you use an extension function in Kotlin?
Kotlin
Copied
7. What is an infix function in Kotlin?
An infix function is a function marked with the infix
keyword that allows a more natural syntax when calling the function.
8. How do you declare an infix function in Kotlin?
Kotlin
Copied
9. What are the requirements for using infix functions in Kotlin?
Infix functions must be member functions or extension functions with a single parameter.
10. How do you call an infix function in Kotlin?
Kotlin
Copied
11. Can you make existing functions infix in Kotlin?
No, you can only declare new functions as infix.
12. What is the purpose of the this
keyword in an infix function?
In infix functions, the this
keyword is optional when accessing the receiver object.
13. How can you define extension properties in Kotlin?
Kotlin
Copied
14. What is the difference between extension functions and member functions?
Extension functions are defined externally, while member functions are part of the class definition.
15. Explain the use of generic types in extension functions.
Kotlin
Copied
16. Can you extend nullable types with extension functions in Kotlin?
Yes, you can extend nullable types, but you need to check for nullability within the extension function.
17. How do you organize extension functions in Kotlin?
Extension functions are often organized into separate files or packages based on their functionality.
18. Explain the concept of extension function conflicts in Kotlin.
If two extension functions with the same name are in scope, the one with the more specific receiver type takes precedence.
19. How can you provide default values for parameters in extension functions?
Kotlin
Copied
20. Can extension functions access private members of a class?
No, extension functions cannot access private members of a class.
21. Explain the use of the @JvmName
annotation in Kotlin extension functions.
The @JvmName
annotation allows you to specify the name of the generated Java method for interoperability.
22. How do extension functions support function chaining in Kotlin?
Kotlin
Copied
23. Can you extend non-public classes with extension functions in Kotlin?
No, extension functions cannot be declared for non-public classes.