11. How do you create a data class in Kotlin, and what benefits does it offer?

  • To create a data class in Kotlin, you use the data modifier before the class keyword. Data classes are designed for storing data and automatically generate useful functions like equals(), hashCode(), and toString(). They simplify working with immutable data and reduce boilerplate code.

12. What are Kotlin’s extension functions, and why do they provide utility?

  • Kotlin’s extension functions allow you to add new functionality to existing classes without modifying their source code. They provide utility by enabling you to extend the capabilities of classes from external libraries or even the standard library. This enhances code readability and reusability.

13. Describe how Kotlin handles checked exceptions compared to Java.

  • Unlike Java, Kotlin does not have checked exceptions. In Kotlin, all exceptions are unchecked, which means you are not required to declare or catch exceptions explicitly. This simplifies exception handling and reduces the verbosity of code.

14. Explain the “Elvis operator” in Kotlin and provide an example of its usage.

  • The Elvis operator (?:) in Kotlin is used to provide a default value when an expression evaluates to null. For example, val result = nullableValue ?: “Default” assigns the value of nullableValue to result if it is not null, or “Default” if it is null.

15. What is smart casting in Kotlin, and how does it work with nullable types?

  • Smart casting in Kotlin is an automatic type casting mechanism that applies when a nullable type has been checked for null. Once null-check is performed, the compiler automatically treats the variable as non-nullable within the corresponding scope, eliminating the need for explicit casting.

16. Describe Kotlin’s object-oriented programming features, such as classes and inheritance.

  • Kotlin supports object-oriented programming with features like classes, inheritance, encapsulation, and polymorphism. Classes are used to define objects and their behavior, and inheritance allows one class to inherit the properties and methods of another, fostering code reusability.

17. How do you use the lateinit modifier in Kotlin, and what limitations does it have?

  • The lateinit modifier is used to indicate that a non-nullable property will be initialized later. It is typically used with properties in classes. However, lateinit properties must be of non-nullable types, and they must be initialized before accessing them, or it will result in a runtime exception.

18. Explain the differences between is and as operators when working with type casting.

  • The is operator is used to check if an object is an instance of a specific type and returns a Boolean. The as operator is used for explicit type casting and may throw a ClassCastException if the cast is not possible. The as? operator performs safe casting, returning null if the cast is not possible.

19. What are sealed classes in Kotlin, and how are they useful for managing hierarchies of types?

  • Sealed classes in Kotlin are used to represent restricted class hierarchies, where all subclasses are known and limited. They are useful for managing hierarchies of types in a way that ensures all possible subclasses are explicitly defined and accounted for, making code more predictable and reliable.

20. What are coroutines in Kotlin, and how do they differ from traditional threads?

  • Coroutines in Kotlin are a lightweight and efficient way to perform asynchronous operations. They differ from traditional threads by being more resource-friendly and offering structured concurrency. Coroutines can be suspended and resumed, enabling efficient use of system resources.