Language Elements

Data Types

Kotlin Nullable Types

Exctension Functions

Object Oriented Kotlin

Data Classes

Coroutines

Collections

Kotlin Example Codes

Kotlin Interview Questions

  • What are the key differences between Java and Kotlin?
  • Kotlin offers features like null safety, extension functions, smart casts, and concise syntax, whereas Java is more verbose and lacks built-in null safety. Kotlin supports functional programming, and it is fully interoperable with Java.

  • List some advantages of Kotlin over Java.
  • Null safety to prevent NullPointerExceptions.
    Concise and expressive syntax.
    Built-in support for coroutines for asynchronous programming.
    Extension functions to enhance existing classes.
    Smart casting and improved type inference.

  • List any advantages of using Java over Kotlin.
  • Larger community and ecosystem.
    More mature tooling and library support.
    Faster compilation in some cases.
    Easier for developers familiar with traditional OOP languages.

  • What are extension functions in Kotlin?
  • Extension functions allow developers to add new functionality to existing classes without modifying their code. For example, `fun String.reverse(): String { return this.reversed() }` adds a reverse function to `String`.

  • What are coroutines?
  • Coroutines are lightweight threads in Kotlin that allow for asynchronous and non-blocking programming. They simplify tasks like network requests or database operations using structured concurrency and functions like `launch` and `async`.

  • What is a data class in Kotlin?
  • A data class is a class in Kotlin specifically designed to hold data. It automatically generates functions like `toString()`, `equals()`, `hashCode()`, and `copy()`. Declared with `data` keyword, e.g., `data class User(val name: String, val age: Int)`.

  • What are higher-order functions in Kotlin?
  • Higher-order functions are functions that take other functions as parameters or return them. For example, `fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { return operation(a, b) }`.

  • How does Kotlin handle null safety?
  • Kotlin provides nullable types using `?` and forces null checks at compile time. For instance, `val name: String? = null` allows a nullable string, and `name?.length` safely accesses its length.

  • What are sealed classes in Kotlin?
  • Sealed classes restrict class hierarchy, meaning subclasses must be defined in the same file. They are useful in representing a fixed set of types, like enums but with more flexibility.

  • What are lazy properties in Kotlin?
  • Lazy properties are initialized only when accessed for the first time. Declared using `val propertyName: Type by lazy { initializer }`, it ensures efficient resource usage.

  • What is the difference between `lateinit` and `lazy` in Kotlin?
  • `lateinit` is used for non-nullable `var` properties that will be initialized later, while `lazy` is used for `val` properties that are initialized only when accessed for the first time. `lateinit` is mutable, whereas `lazy` is read-only.

  • What are delegates in Kotlin?
  • Delegates allow a property’s behavior to be defined in a reusable way. Examples include `lazy` for lazy initialization, `observable` for listening to property changes, and `by` for delegation to another object or property.

  • What is the purpose of the `by` keyword in Kotlin?
  • The `by` keyword is used for delegation. It delegates the implementation of an interface or property to another object. For example, `class DelegateExample(val delegate: Delegate) : Interface by delegate`.

  • What is the difference between `val` and `var` in Kotlin?
  • `val` is used for read-only properties (immutable), while `var` is used for mutable properties. A `val` property cannot be reassigned after initialization.

  • What are companion objects in Kotlin?
  • Companion objects are singletons within a class that provide functionality similar to static members in Java. They are declared using `companion object` and can access private members of the containing class.

  • What is the use of the `object` keyword in Kotlin?
  • The `object` keyword is used to create singletons, anonymous classes, or companion objects. For example, `object Singleton { fun doSomething() { ... } }` creates a singleton instance.

  • What are inline functions in Kotlin?
  • Inline functions reduce overhead by copying the function’s code directly to the call site. This is especially useful for higher-order functions. Declared with the `inline` keyword, they improve performance by avoiding function call overhead.

  • What is the difference between `apply`, `let`, `also`, `run`, and `with` in Kotlin?
  • These are scope functions used to operate on objects: - `apply`: Returns the object and is used for configuring properties. - `let`: Returns a result and is used for performing actions on the object. - `also`: Returns the object but is used for side effects. - `run`: Returns a result and is used with a lambda receiver. - `with`: Operates on an object but is not an extension function.

  • What are type aliases in Kotlin?
  • Type aliases provide an alternative name for existing types, making complex or verbose types easier to use. Declared using `typealias`, e.g., `typealias StringList = List`.

  • What is the purpose of `@JvmStatic`, `@JvmOverloads`, and `@JvmName` annotations in Kotlin?
  • These annotations improve Java interoperability: - `@JvmStatic`: Makes a Kotlin function callable as a static method in Java. - `@JvmOverloads`: Generates multiple overloaded methods for default parameters. - `@JvmName`: Changes the Java name of a Kotlin method or property.






    Copyright © by Zafar Yasin. All rights reserved.