• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

Question - 81 : - What are the benefits of using a Sealed Class over Enum?

Answer - 81 : -

Sealed classes give us the flexibility of having different types of subclasses and also containing the state. The important point to be noted here is the subclasses that are extending the Sealed classes should be either nested classes of the Sealed class or should be declared in the same file as that of the Sealed class.

Question - 82 : - Why kotlin is better than Java?

Answer - 82 : -

Kotlin is a simple general-purpose language, in which there is a code redundancy when compared to java. There are many features in kotlin like null safety, extension functions, primary constructors, inline or lambda expressions, properties, and type interference for the properties and variables.

Question - 83 : - Explain functions in kotlin?

Answer - 83 : -

Functions used in kotlin are simple to store in data structures and variables and can possess arguments that are passed from the high ordered data functions.

 An example of the sample function declaration in kotlin:

function double(y:int): 
Int
 {
    return 3 * y
  }
val reslt = double(3)

Question - 84 : - What are the high order functions?

Answer - 84 : -

High order functions consider functions as a parameter and produce a function.

Question - 85 : - How to convert a String into an int in the kotlin?

Answer - 85 : -

To convert a string value to the string value to it in kotlin we use point() method.

Let us see an example: 

function main(args: array) 
{
    val s: str]ng = "Kotlin"
    var y = 10
    y = "10".toint()
}

Question - 86 : - List out the basic data types we use in kotlin?

Answer - 86 : -

The data type of a constant or a variable decides what is the type of a variable and how much space needs to store. Types of data that a string allow are:

  • Strings
  • Numbers
  • Arrays
  • Booleans
  • Characteristics

Question - 87 : - What is the difference between and interface and an abstract class?

Answer - 87 : - Though both may seem very similar they are in fact very different and serve very different purposes. Abstract classes are meant to serve the purpose of Generalizing behavior while interfaces are meant to serve the purpose of Standardizing behavior.

Question - 88 : - What is the difference between Java field and Kotlin property?

Answer - 88 : -

This is an example of a Java field:

public String name = "Marcin";
Here is an example of a Kotlin property:

var name: String = "Marcin"
They both look very similar, but these are two different concepts. Direct Java equivalent of above Kotlin property is following:

private String name = "Marcin";
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
The default implementation of Kotlin property includes field and accessors (getter for val, and getter and setter for var). Thanks to that, we can always replace accessors default implementation with a custom one.

Question - 89 : - What is the difference between launch/join and async/await in Kotlin coroutines?

Answer - 89 : -

launch is used to** fire and forget coroutine**. It is like starting a new thread. If the code inside the launch terminates with exception, then it is treated like uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications. join is used to wait for completion of the launched coroutine and it does not propagate its exception. However, a crashed child coroutine cancels its parent with the corresponding exception, too.

async is used to start a coroutine that computes some result. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed. You MUST NOT forget about the coroutine you’ve started with async.

Question - 90 : - How Kotlin coroutines are better than RxKotlin/RxJava?

Answer - 90 : -

Kotlin coroutines are different from Rx. Both are designed to address a problem of asynchronous programming, however their approach to solution is very different:

  • Rx comes with a particular functional style of programming that can be implemented in virtually any programming language without support from the language itself. It works well when the problem at hand easily decomposes into a sequence of standard operators and not so well otherwise.

  • Kotlin coroutines provide a language feature that let library writers implement various asynchronous programming styles, including, but not limited to functional reactive style (Rx). With Kotlin coroutines you can also write your asynchronous code in imperative style, in promise/futures-based style, in actor-style, etc.
How Kotlin coroutines are better than RxKotlin? You just write sequential code, everything is as easy as writing synchronous code except it execute asynchronously. It's easier to grasp.

Coroutines are better to deal with resources

  • In RxJava you can assign computations to schedulers but subscribeOn() and ObserveOn()are confusing. Every coroutine is given a thread context and return to parent context. For a channel, both side (producer, consumer) execute on his own context. Coroutines are more intuitive on thread or thread pool affectation.
  • Coroutines give more control on when those computation occur. You can for example pass hand (yield), prioritize (select), parallelize (multiple producer/actor on channel) or lock resource (Mutex) for a given computation. It may not matter on server (where RxJava came first) but on resources limited environment this level of control may be required.
  • Due to it's reactive nature, backpressure doesn't fit well in RxJava. In the other end send() to channel is a suspensive function that suspend when channel capacity is reached. It's out-of-the-box backpressure given by nature. You could also offer() to channel, in which case the call never suspend but return false in case the channel is full, effectively reproducing onBackpressureDrop() from RxJava. Or you could just write your own custom backpressure logic, which won't be difficult with coroutines, especially compared to do the same with RxJava.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners