Extension & Infix

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:

Codecastic
0
Codecastic
0
Codecastic
0
Codecastic
0

On this page

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?

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?

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?

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?

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?

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.

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?

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?

Copied


23. Can you extend non-public classes with extension functions in Kotlin?

No, extension functions cannot be declared for non-public classes.


Last updated -

Share this page

On this page
Extensions and Infix Functions
1. What is an extension function in Kotlin?
2. How do you declare an extension function in Kotlin?
3. Can you extend final classes with extension functions in Kotlin?
4. Explain the purpose of the this keyword in an extension function.
5. What are the visibility modifiers that can be applied to extension functions?
6. How do you use an extension function in Kotlin?
7. What is an infix function in Kotlin?
8. How do you declare an infix function in Kotlin?
9. What are the requirements for using infix functions in Kotlin?
10. How do you call an infix function in Kotlin?
11. Can you make existing functions infix in Kotlin?
12. What is the purpose of the this keyword in an infix function?
13. How can you define extension properties in Kotlin?
14. What is the difference between extension functions and member functions?
15. Explain the use of generic types in extension functions.
16. Can you extend nullable types with extension functions in Kotlin?
17. How do you organize extension functions in Kotlin?
18. Explain the concept of extension function conflicts in Kotlin.
19. How can you provide default values for parameters in extension functions?
20. Can extension functions access private members of a class?
21. Explain the use of the @JvmName annotation in Kotlin extension functions.
22. How do extension functions support function chaining in Kotlin?
23. Can you extend non-public classes with extension functions in Kotlin?