-
How do you declare and use lambdas in Kotlin?
- Lambdas are declared using curly braces and can be passed as arguments to functions. They provide a concise way to define small, anonymous functions.
-
Explain ‘Smart Cast’ in Kotlin.
- ‘Smart Cast’ is a feature in Kotlin that automatically casts a variable after a type check, eliminating the need for explicit casting.
-
How does Kotlin support the ‘Single Responsibility Principle’ in design patterns?
- Kotlin promotes the ‘Single Responsibility Principle’ by encouraging concise functions and classes, which makes it easier to adhere to this principle.
-
What are the key differences between ‘val’ and ‘const val’ in Kotlin?
- ‘val’ is used for runtime constants, whereas ‘const val’ is used for compile-time constants. ‘const val’ values must be known at compile time.
-
Explain the purpose of the ‘run’ function in Kotlin.
- The ‘run’ function is used to execute a lambda expression within the context of an object, and it returns the result of the lambda. This can make code more readable.
-
What is Kotlin’s approach to functional programming, and how does it differ from Java?
- Kotlin fully supports functional programming constructs like lambdas, higher-order functions, and immutability, making functional programming more natural compared to Java.
-
How do you use the ‘also’ function in Kotlin?
- The ‘also’ function is used for performing additional actions on an object within a lambda. It returns the original object, facilitating method chaining and improving code readability.
-
What is the ‘withIndex’ extension function used for in Kotlin?
- The ‘withIndex’ function is used to iterate over a collection while also having access to the index of each element. This is useful when you need both the element and its position.
-
Explain ‘extension properties’ in Kotlin.
- Extension properties allow you to add new properties to existing classes, enhancing their functionality without modifying their source code.
-
What is the purpose of the ‘takeIf’ and ‘takeUnless’ functions in Kotlin?
- ‘takeIf’ returns the object if a condition is met, or null otherwise. ‘takeUnless’ returns the object if the condition is not met, or null if it is. These functions are helpful for filtering data.
-
How does Kotlin promote immutability, and why is it important?
- Kotlin encourages immutability through the use of ‘val’ and data classes. Immutability enhances code stability and prevents unintended side effects.
-
Explain ‘infix’ functions in Kotlin.
- Infix functions allow you to call functions with a single argument using infix notation (no dot or parentheses). This can lead to more readable code.
-
What is the ‘takeUnless’ function used for in Kotlin?
- The ‘takeUnless’ function returns the object if a condition is not met, or null if it is met. It’s the inverse of ‘takeIf’ and is used for filtering data.
-
How do you create a custom DSL (Domain-Specific Language) in Kotlin?
- You can create a custom DSL in Kotlin by leveraging the power of extension functions and lambdas to define a language specific to your problem domain.
-
Explain the purpose of the ‘runCatching’ function in Kotlin.
- ‘runCatching’ is used to safely execute code that may throw exceptions. It returns a result containing either the success value or the exception, improving error handling.