Kotlin Interview Questions Part 6

Emine Şa
4 min readSep 2, 2024

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

Question 51: What is the Singleton Pattern?

The Singleton Pattern is a design pattern that ensures a class has only one instance and provides a global point of access to that instance. This pattern guarantees that a class has only one instance throughout the application, and it allows access to that instance from anywhere in the code.

Question 52:How can we create a Singleton in Kotlin?

In Kotlin, the object keyword is used to create a singleton. This means that only one instance of the class is created, and this instance is accessible from anywhere in the application. For example:

object Singleton {
val name = "Only Instance"
}

This structure works like static classes and provides access to the data without creating a new instance repeatedly.

Question 53: What are the different uses of object in Kotlin?

  1. Object Declaration (Singleton) — Creates a single instance of a class. This ensures that only one instance is created and can be accessed globally.
  2. Object Expression (Anonymous Class) — Creates an anonymous class. This is useful for defining a class on the fly without naming it.
  3. Companion Object — Associates static members with a class. This allows you to define methods and properties that are accessed via the class itself rather than instances.
  4. Object Interface Implementation — Provides an anonymous implementation of an interface. This allows you to implement an interface without creating a named class.

Question 54: When is an Object Expression used?

An object expression is typically used when you want to create an anonymous instance of an interface or an abstract class. This usage allows you to create an object directly without naming a class. It is useful for situations where a simple, unnamed implementation is needed.

val myObject = object : Runnable {
override fun run() {
println("Running in anonymous class")
}
}

// Kullanımı
myObject.run() // "Running in anonymous class"

Question 55: How can the Singleton Pattern be made thread-safe?

In Java, we can make a Singleton instance thread-safe using double-checked locking. The volatile keyword ensures that the instance is correctly visible to all threads. First, we check if the instance is null. If it is, we then enter a synchronized block and check again to create the instance if it is still null. This approach minimizes the overhead of acquiring the lock while ensuring that only one instance is created.

public class Singleton {

private static volatile Singleton instance;

private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

In Kotlin, the object keyword directly creates a thread-safe Singleton pattern. A class defined with object is instantiated only once by the JVM and remains a single instance throughout the application.

Question 56: Why is the Singleton pattern sometimes considered an anti-pattern?

The Singleton pattern is sometimes considered an anti-pattern because it creates a static class. Static classes are not managed by the Garbage Collector, meaning they are not automatically cleaned up from memory. Improper use of Singletons can lead to memory bloat, resulting in excessive RAM consumption and potentially causing the application to crash.

Question 57: What is a Garbage Collector?

The Garbage Collector is a system component that automatically removes variables and objects from memory when they are no longer used for a certain period. This process is managed by the system rather than manually by the developer.

Question 58: What is a Memory Leak?

A memory leak occurs when a program continues to hold onto data that is no longer in use, preventing the system from reclaiming it. As this data accumulates, memory capacity becomes exhausted, leading to performance issues, slowdowns, and potentially causing the application to crash.

Memory leaks are often caused by:

  • Improper reference management: If references to unused objects are not cleaned up, they cannot be removed from memory.
  • Circular references: When objects reference each other, the Garbage Collector may not be able to release them.
  • Singletons and static variables: If not managed properly, these can persist in memory unnecessarily.

Question 59: What is a Companion Object?

In Kotlin, a companion object is used to add static members to a class. It is similar to the static keyword in Java, but in Kotlin, these members are defined within the class. A class can have only one companion object, and it can be accessed using the class name or a custom name without creating an instance of the class.

Question 60: What are the similarities and differences between Companion Object and object in Kotlin?

Similarities:

  • Both object and companion object are used in Kotlin to create singleton instances.
  • Both can be accessed directly without creating an instance of the class.

Differences:

  • An object created with the object keyword is a direct singleton instance of a class and is globally accessible.
  • A companion object resides within a class and is used to define static members associated with that 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

No responses yet