Kotlin Technical Interview Questions Flashcards

1
Q

How do you create a Singleton in Kotlin?

A

Use the “object” keyord (e.g. object someSingleton)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to initialize an array in Kotlin with values?

A

val numbers: IntArray = intArrayOf(1, 2, 3, 4, 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a data class in Kotlin?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between var & val in Kotlin?

A

var - a general, mutable variable

val - like a final, immutable variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where should val & var be used?

A

Use var where the a value is going to change frequently

Use val where there is no change in the value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the advantages of when vs. switch in Kotlin

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the null safety in Kotlin

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is it recommended to create constants in Kotlin?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why should you not use companion objects with constants?

A

It is a more expense call to instance methods (getters/setters) than calling static methods. Instead, use objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can you use IntArray & Array in Kotlin interchangeably?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Coroutines in Kotlin?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the advantages of Kotlin over Java?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is lateinit in Kotlin & when would you use it?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some useful cases for lateinit?

A
  • Android: variables that get initialized in lifecycle methods
  • Using Dagger for DI
  • Setup for unittests
  • Spring boot annotations
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of Companion Objects in Kotlin?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the kotlin double-bang (!!) operator?

A

The double-bang operator is the not-null assertion operator - it converts any value to a non-null type & throws a KotlinNullPointerException if the value is null.

This operator should be used in cases where you need to 100% guarantee that a value is not null.

17
Q

What is the difference between const and val?

A

“const”s are compile-time constants & can never be assigned a function or class constructor - only String or primitives

“val”s are compiled/assigned at runtime & can be assigned primitives, functions, &/or class constructors

18
Q

What is the difference between open & public in Kotlin?

A

open - means open for extension; allows others to inherit from the class

public - default visibility modifier

19
Q

What is the difference between suspending and blocking?

A

Blocking - means that a call to any other function from the same thread will halt the parent’s execution. This also means that if a blocking call is made on the main thread, you effectively freeze the UI

Suspending - doesn’t necessarily block the parent function’s execution. If you call a suspending function in some thread, you can easily push that function to a different thread. In case it is a heavy operation, it won’t block the main thread. If the suspending function has to suspend, it will simply pause its execution, freeing up its thread for other work. Once it’s done suspending, it will get the next free thread from the pool to finish its work.

20
Q

What is the equivalent of Java static methods in Kotlin?

A

Using functions within a companion object for a class.

21
Q

When to use lateinit over lazy initialization in Kotlin?

A

Lateinit:

  • If properties are mutable
  • If properties are set externally (e.g. need to pass in some external value to set it)

Lazy:
- If they are only meant to be initialized once & shared by all, & it’s more internally set

22
Q

Compare lateinit vs Lazy in Kotlin

A

lateinit var:

  • Can be intialized from anywhere the object is seen from
  • Multiple initialization possible
  • Non-thread safe
  • Can only be used for var
  • Not eligible for nonnull properties
  • An isInitialized method added to check whether the value has been initialized before
  • Not allowed on properties of primitive types

by lazy:

  • Can only be initialized from the initializer lambda
  • Only initialized a single time
  • Thread-safe by default & guarantees that the initializer is invoked once
  • Can only be used for val
  • not eligible for nonnull properties
  • Property never able to un-initialize
  • Allowed on properties of primitive types.