Kotlin Technical Interview Questions Flashcards
How do you create a Singleton in Kotlin?
Use the “object” keyord (e.g. object someSingleton)
How to initialize an array in Kotlin with values?
val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)
What is a data class in Kotlin?
A class with the express purpose of holding data, with the following requirements:
- The primary constructor needs to have at least one parameter
- All primary constructor parameters need to be marked as val or var
- Data classes cannot be abstract, open, sealed, or inner
What is the difference between var & val in Kotlin?
var - a general, mutable variable
val - like a final, immutable variable
Where should val & var be used?
Use var where the a value is going to change frequently
Use val where there is no change in the value
Explain the advantages of when vs. switch in Kotlin
- Has a better design
- More concise & powerful than a traditional switch
- Can be used either as an expression or as a statement
- usage examples:
- 2 or more choices
- “when” without arguments
- Any type can be passed into when
- Smart casting (via “is” comparator)
- Ranges
Explain the null safety in Kotlin
Kotlin’s type safety system is aimed at eliminating the danger of null references from code (aka the Billion Dollar Mistake). In Kotlin, the type system distinguishes between references that can hold null (nullable references) & those that cannot (non-null references)
How is it recommended to create constants in Kotlin?
val MY_CONSTANT = “constant1”
const val MY_CONSTANT = “constant2”
The difference here is that while both are immutable (via val), the “const” keyword is used for variables that are known at compile-time.
Why should you not use companion objects with constants?
It is a more expense call to instance methods (getters/setters) than calling static methods. Instead, use objects
Can you use IntArray & Array in Kotlin interchangeably?
NO - Array is Integer[] under the hood, while IntArray is int[].
This means that when an Int is put into an Array, it will always be boxed, whereas with IntArray, no boxing will occur b/c it translates to a Java primitive array
What are Coroutines in Kotlin?
Coroutines is a rich library that contains a number of high-level coroutine enabled primitives (like launch & async) & provides an API to write async code sequentially.
Coroutines act like light-weight threads that use predefined thread pools & smart scheduling & thus, doesn’t actually allocate new threads. Additionally, coroutines can be suspended & resumed mid-execution.
What are the advantages of Kotlin over Java?
- Data Classes vastly reduce getter/setter setup code from Java
- Abstract vs. Open classes: Open class lets you make an inheritable class while also being usable itself
- Extension functions
- Null: Kotlin allows you to decide what can & can’t be null
- Singleton: object instead of a class
- Generics: Reified generics (actual usage of the type), in & out for covariance
- Named parameters for API back compatibility preservation
- Primary constructor: able to write constructor without any additional functions or extra declarations
What is lateinit in Kotlin & when would you use it?
Lateinit - means late initialization; If you don’t want to initialize a variable in the constructor, you can instead initialize it later on (while guaranteeing that it’s initialized before you use it). It will not allocate memory until it’s initialized, however you cannot use lateinit for primitive types
What are some useful cases for lateinit?
- Android: variables that get initialized in lifecycle methods
- Using Dagger for DI
- Setup for unittests
- Spring boot annotations
What is the purpose of Companion Objects in Kotlin?
Since Kotlin doesn’t have static members/member functions, if you need to write a function that cal be called w/o having a class instance but needs access to the itnernals of a class, you can write it as a member of a companion object declaration inside a class.
The companion object is a Singleton, a proper object on its own, & can have its own supertypes