• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

Question - 21 : - What are the advantages of "when" over "switch" in Kotlin?

Answer - 21 : -

The "switch" is used in Java, but in Kotlin, that switch gets converted to "when". When has a better design as compared to "switch", and it is more concise and powerful than a traditional switch. We can use "when" either as an expression or as a statement.

Following are some examples of when usage in Kotlin:

In two or more choices:

when(number) {  
    1 -> println("One")  
    2, 3 -> println("Two or Three")  
    4 -> println("Four")  
    else -> println("Number is not between 1 and 4")  
}  
"when" without arguments:

when {  
    number < 1 -> print("Number is less than 1")  
    number > 1 -> print("Number is greater than 1")  
}  
Any type passed in "when":

fun describe(obj: Any): String =  
    when (obj) {  
      1 -> "One"  
      "Hello" -> "Greeting"  
      is Long -> "Long"  
      !is String -> "Not a string"  
      else -> "Unknown"  
    }  
Smart casting:

when (x) {  
    is Int -> print("X is integer")  
    is String -> print("X is string")  
}  
Ranges:

when(number) {  
    1 -> println("One") //statement 1  
    2 -> println("Two") //statement 2  
    3 -> println("Three") //statement 3  
    in 4..8 -> println("Number between 4 and 8") //statement 4  
    !in 9..12 -> println("Number not in between 9 and 12") //statement 5  
    else -> println("Number is not between 1 and 8") //statement 6  
}  

Question - 22 : - What do you understand by the Null safety in Kotlin?

Answer - 22 : -

In Kotlin, the main motive of the type system is to eliminate the danger of null references from code. It is also known as the Billion Dollar Mistake.

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java, this would be the equivalent of a NullPointerException.

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). For example, a regular variable of type String can not hold null:

var a: String = "abc"  
a = null // compilation error  
To allow nulls, we can declare a variable as nullable string, written String?:

var b: String? = "abc"  
b = null // ok  
print(b)  

Question - 23 : - Do we have a ternary operator in Kotlin just like Java?

Answer - 23 : -

No. In Kotlin, we don't have a ternary operator like Java, but we can use the functionality of the ternary operator by using if-else or Elvis operator.

Question - 24 : - What is Elvis operator in Kotlin?

Answer - 24 : -

In Kotlin, we can assign null values to a variable using the null safety property. To check if a value has null value, we can use if-else or can use the Elvis operator i.e. ?:

For example:

var name:String? = "Mindorks"  
val namenameLength = name?.length ?: -1  
println(nameLength)  
In the above example, the Elvis operator(?:) we are using will return the length of the name if the value is not null; otherwise, if the value is null, then it will return -1.

Question - 25 : - Why is Kotlin interoperable with Java?

Answer - 25 : -

Kotlin is interoperable with Java because it uses JVM bytecode. It provides the facility to compile it directly to bytecode that helps to achieve faster compile-time and makes no difference between Java and Kotlin for JVM.

Question - 26 : - What do you understand by lazy initialization in Kotlin?

Answer - 26 : -

Kotlin provides the facility of lazy initialization, which specifies that your variable will not be initialized unless you use that variable in your code. It will be initialized only once. After that, you use the same value.

In lazy initialization, the lazy() function is used that takes a lambda and returns an instance of lazy, which can serve as a delegate for implementing a lazy property: the first call to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.

val test: String by lazy {  
    val testString = "some value"  
}  

Question - 27 : - How many types of constructors are used in Kotlin?

Answer - 27 : -

There are two types of constructors available in Kotlin:

  • Primary constructor
  • Secondary constructor

Question - 28 : - What is Lateinit in Kotlin, and when is it used?

Answer - 28 : -

Lateinit means late initialization. It is used when you do not want to initialize a variable in the constructor and instead initialize it later.

You should declare that variable with lateinit keyword to guarantee the initialization, not before using it. It will not allocate memory until it is initialized. You cannot use lateinit for primitive type properties like Int, Long, etc.

lateinit var test: String  
fun doSomething() {  
    test = "Some value"  
    println("Length of string is "+test.length)  
    test = "change value"  
}  
This is mainly used in the following cases:

  • Android: variables that get initialized in lifecycle methods.
  • Using Dagger for DI: injected class variables are initialized outside and independently from the constructor.
  • Setup for unit tests: test environment variables are initialized in a @Before - annotated method.
  • Spring Boot annotations (e.g., @Autowired).

Question - 29 : - How can we convert a Kotlin source file to a Java source file?

Answer - 29 : -

Follow the steps given below to convert your Kotlin source file to a Java source file:

  • First, open your Kotlin project in the IntelliJ IDEA / Android Studio.
  • Then navigate to Tools > Kotlin > Show Kotlin Bytecode.
  • Now, click on the Decompile button to get your Java code from the bytecode.

Question - 30 : - What kinds of programming types does Kotlin support?

Answer - 30 : -

Kotlin supports the following programming types:

  • Procedural Programming
  • Object-Oriented Programming


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners