-
What are Kotlin’s ‘delegated properties,’ and how do they work?
- Delegated properties allow you to delegate the handling of property access and modification to another object. They simplify common tasks and reduce boilerplate code.
-
How do you perform lazy initialization in Kotlin?
- You can use the ‘lazy’ delegate to perform lazy initialization. The initialization code is executed only once when the property is first accessed.
-
Explain ‘Type Aliases’ in Kotlin and when to use them.
- Type aliases provide alternative names for existing types. They improve code readability, especially when dealing with complex types or generics.
-
What is the ‘let’ function used for in Kotlin?
- The ‘let’ function is used to execute a block of code on a non-null object, improving code readability and ensuring null safety.
-
Explain ‘sealed interfaces’ in Kotlin and their purpose.
- Sealed interfaces are used to restrict the number of implementers. They are useful when you want to limit which classes can implement an interface.
-
How do you implement a ‘Comparator’ in Kotlin?
- To implement a ‘Comparator’ in Kotlin, you can define a custom comparison function and use it with the ‘sortedWith’ function or a sorting method.
-
What is the ‘reified’ keyword in Kotlin, and when is it used?
- ‘Reified’ is used with inline functions to retain type information at runtime. It’s typically used when working with generics and reflection.
-
Explain the purpose of the ‘expect’ and ‘actual’ declarations in Kotlin Multiplatform Mobile (KMM).
- ‘expect’ is used in the common code to declare an expected interface, while ‘actual’ is used in platform-specific code to provide the actual implementation. This enables code sharing across platforms.
-
What are the benefits of using ‘Sealed Classes’ in Kotlin?
- Sealed classes are useful for defining a restricted hierarchy of classes. They ensure that all possible subclasses are known and make code more maintainable by handling all cases.
-
How do you use the ‘flatMap’ function in Kotlin?
- The ‘flatMap’ function is used to transform and flatten nested collections or sequences. It’s particularly helpful when working with collections of collections.
-
What is the ‘coalesce’ function in Kotlin, and how is it used?
- The ‘coalesce’ function is used to choose the first non-null value from a list of expressions. It simplifies conditional checks and returns the first non-null value.
-
Explain ‘Sealed Class Hierarchies’ in Kotlin and when to use them.
- Sealed class hierarchies are used to represent a finite set of classes. They are particularly useful when working with when expressions for exhaustive checks.
-
How does Kotlin support functional programming concepts like map, filter, and reduce?
- Kotlin provides high-order functions like ‘map’, ‘filter’, and ‘reduce’ to work with collections, making it easier to apply functional programming principles.
-
What is the purpose of the ‘withLock’ function in Kotlin’s coroutines?
- The ‘withLock’ function is used to ensure mutual exclusion when working with shared resources in a coroutine. It simplifies the implementation of critical sections.
-
Explain the ‘Delegates.observable’ property delegate in Kotlin.
- ‘Delegates.observable’ allows you to observe changes to a property and react accordingly. It simplifies the implementation of property change listeners.
-
How do you implement a custom scope function in Kotlin, and when is it useful?
- You can implement a custom scope function by defining an extension function on a specific type. It’s useful when you need to encapsulate a specific context for your code.
-
What are ‘extension lambdas’ in Kotlin, and how do they differ from regular lambdas?
- ‘Extension lambdas’ are regular lambdas defined in a specific receiver type. They allow you to extend the functionality of that type with additional lambdas.
-
Explain ‘Contravariance’ and ‘Covariance’ in Kotlin’s type system.
- Contravariance allows for more generic supertypes, while covariance allows for more specific subtypes. Understanding these concepts is important when working with generics.
-
What are ‘inline classes’ in Kotlin, and why are they useful?
- ‘Inline classes’ are used to wrap a single primitive value without introducing runtime overhead. They are useful for creating type-safe abstractions.
-
How do you use the ‘GroupingBy’ function to group elements in a collection in Kotlin?
- The ‘GroupingBy’ function is used to group elements in a collection based on a specific criterion. It simplifies data grouping and aggregation tasks.