Kotlin Interview Questions Part 5

Emine Şa
3 min readAug 26, 2024

Heyy🖖🏽 I hope everything is going well. In this article, we will check out 41–50 Q&A. HERE WE GO 🪅

Question 41: What does “first-class citizen” mean?

In Kotlin, functions are considered first-class citizens. This means that functions can be passed as parameters, stored in variables, and returned from other functions. Simply put, functions can be treated as values, allowing them to be assigned, passed, and returned just like any other data type.

Question 42: What are interfaces used for?

Interfaces are used to allow the same function to work differently in various classes. This way, each class can create customized functions tailored to its specific needs. For example, by defining an Animal interface, we can have each animal produce its unique sound through the makeSound() function. The Cat and Dog classes can implement this interface, each providing a different implementation of the makeSound() function.

Question 43: What are the differences between an interface and an abstract class?

  • Interfaces are implemented, while abstract classes are inherited.
  • Interfaces cannot hold state.
  • Abstract classes have constructors.
  • A class can implement multiple interfaces but can inherit only one abstract class.
  • In abstract classes, you must override any abstract methods.

Question 44: Do functions in interfaces always need to be overridden?

If a function in an interface does not have a body, any class that implements the interface must override the function. However, if the function has a body (i.e., a default implementation is provided), then overriding it is not mandatory.

Question 45: What is a data class and what is it used for?

The data class is a special type of class that is designed to hold and manage data. It must contain at least one property, which can be declared using either val or var. This automatically generates getter and setter methods for the properties. Additionally, data classes provide default implementations for equals(), hashCode(), and toString() methods. You can also use the copy() function to create new instances based on an existing object.

Question 46: What is the difference between data classes and data objects?

  • A data class creates a new instance every time it is instantiated, while a data object is a singleton, meaning there is only one instance.
  • A data class can accept parameters, but a data object cannot.
  • Different instances of a data class can hold different data, whereas a data object holds fixed data that remains constant.

Question 47: What are enum classes used for?

Enum classes are used to represent a group of related constant values in a structured way. For example, they can be used to group constants such as days of the week or user roles. This organization makes the code more orderly and easier to understand.

enum class Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Question 48: What are sealed classes used for?

Sealed classes are used to group related classes into a single structure. Unlike enum classes, sealed classes allow you to define which subclasses can exist at compile time. This provides more safety and control when working with these subclasses.

sealed class Result

data class Success(val data: String) : Result()
data class Failure(val error: String) : Result()
object Loading : Result()

Question 49: What is the relationship between sealed classes and when expressions?

Sealed classes work well with when expressions because they ensure that all possible cases are handled. Since sealed classes define a closed hierarchy of subclasses, the Kotlin compiler can check that all subclasses are covered in a when expression. If any case is missing, the compiler will provide a warning, ensuring that all possibilities are addressed safely.

Question 50: What is the difference between a nested class and an inner class?

  • Nested Class: A nested class is defined within another class and is typically static. You can create an instance of a nested class directly without needing an instance of the outer class.
  • Inner Class: An inner class is also defined within another class but is associated with an instance of the outer class. To create an instance of an inner class, you need an instance of the outer class.

In summary, a nested class is independent of the outer class, while an inner class is tied to an instance of the outer class.

If you have any additional information comment below🪄 Stay tuned for the next post! 🎉

--

--

Emine Şa
Emine Şa

Written by Emine Şa

Software Engineer | Android, Kotlin

Responses (2)