Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Kotlin Interview Questions and Answers

Freshers / Beginner level questions & answers

Ques 1. What is Kotlin?

Kotlin is an open-source, general-purpose, statically typed programming language. It is JVM-based and may be used in all projects where Java is currently used.

It may be used to create Android apps, server-side apps, and a variety of other applications. It is an improved version of Java and is completely interoperable with it. We can even mix Kotlin and Java in the same project.

It blends Object Oriented Programming (OOPs) and functional programming in a platform that is unlimited, self-contained, and unique. It also enables functional twinning using microcodes. 

Is it helpful? Add Comment View Comments
 

Ques 2. Who has created Kotlin?

The JetBrains team created Kotlin. The language was initially launched in February 2016 after a project began in 2010 to develop it. The Apache 2.0 license was used to create Kotlin. 

Is it helpful? Add Comment View Comments
 

Ques 3. Tell some good features about Kotlin?

  • Programs written in Kotlin do not require semicolons. This makes the code more readable and easy to understand.
  • This language enables the interchange and usage of Java in a variety of ways.
  • Furthermore, code written in Java and Kotlin can coexist in the same project.
  • The type system of Kotlin is designed to eliminate NullPointerException from code.
  • Writing new code in Kotlin will take you less time.
  • Kotlin code is even easier to deploy and manage at scale.

Is it helpful? Add Comment View Comments
 

Ques 4. Tell me about the top features of Kotlin.

  • Compact code: Kotlin is an OOPs-based programming language with code lines that may be reduced by up to 40% when compared to Java, making it an excellent choice for software development.
  • Open Source: Kotlin for Android is open-source and uses the JVM to combine the benefits of OOPs and functional programming.
  • Simple Language: When working with Kotlin, compiling the code is simple, resulting in improved performance for Android development. It also explains which types of data functions can be used throughout the code.
  • High number of extensions: Without modifying the code, Kotlin may support a variety of extension functions. It aids developers in making existing code more appealing and wonderful.
  • Full Java Interoperability: Java code can utilize Kotlin code, and Kotlin code can use Java code. So, if you're familiar with OOPS programming, switching to Kotlin development is simple. Also, if there are any Java-based applications, they can be used with Kotlin's environment.
  • Smart Cast: Smart casting is a technique that reduces the cost of an application while also improving its speed and performance. It uses typecasting or immutable data to manage the efficiency of programming. This technique is supported by Kotlin.
  • Low Learning Curve: Kotlin is preferred by businesses due to its low adoption cost. Most significantly, it is simple for developers to learn, especially if they have programming experience.

Is it helpful? Add Comment View Comments
 

Ques 5. What are the various data types available in Kotlin? Explain them.

Primitive data types are the most basic data types in Kotlin, and all others are reference types like array and string. Kotlin contains all data types as objects. Following are the different data types available in Kotlin:

Integer Data Type -

Data TypeSpace Required
byte8 bits
short16 bits
int32 bits
long64 bits

Floating Point Data Type - 

Data TypeSpace Required
float32 bits
double64 bits

Boolean Data Type - 

True or false is the only bit of information represented by the Boolean data type. In Kotlin, the Boolean type is the same as in Java. 

Data TypeSpace Required
boolean1 bit

Character Data Type - 

Small letters (a-z), capital letters (A-Z), numerals (0-9), and other symbols are represented by the character data type.

Data TypeSpace Required
char8 bits

String Data Type -

Strings are represented in Kotlin by the type String. A string value is often a sequence of characters enclosed in double quotations ("). The space required in this case depends on the number of characters in the string.

Array Data Type -

The Array class in Kotlin is used to represent arrays. It has the get and set functions that, due to operator overloading conventions, can be used as  ‘[]’  as well. The space required by the array also depends on the number of elements it posses.

Is it helpful? Add Comment View Comments
 

Ques 6. How are variables declared in Kotlin? What are the different types of variables in Kotlin? Explain with examples.

Every variable in Kotlin must be declared before it can be used. An attempt to use a variable without declaring it results in a syntax error. The type of data you are authorised to put in the memory address is determined by the variable type declaration. The type of variable can be determined from the initialised value in the case of local variables.

For example,

var site = "interviewbit"

The above code declares a variable “site” of type String because the value with which the variable is initialised is a String.

  • Immutable Variables — Immutable variables are also known as read-only variables. They are declared using the val keyword. Once these variables have been declared, we cannot change their values.

The syntax is as follows :

val variableName = value

For example,

val sample = "interview"
sample = "interviewbit" // results in compile time error

The second line in the above code snippet would result in a compile-time error as expected.

Because it can be initialized with the value of a variable, an immutable variable is not a constant. It means that the value of an immutable variable does not need to be known at compile-time and that if it is defined inside a construct that is called several times, it can take on a different value with each function call. For example, 

var sample = "interview"
val newSample = sample // no compile time error

The above code snippet runs fine and does not produce any errors.

  • Mutable Variables - In a mutable variable, the value of the variable can be changed. We use the keyword “var” to declare such variables.

The syntax is as follows :

var variableName = value

For example,

var sample = "interview"
sample = "fun" // no compile time error

The above code snippet runs fine and does not produce any errors.

Is it helpful? Add Comment View Comments
 

Ques 7. What are data classes in Kotlin? Explain with a proper example.

The Data class is a simple class that holds data and provides typical functions. To declare a class as a data class, use the data keyword. 

Syntax:

data class className ( list_of_parameters)

The following functions are automatically derived by the compiler for the data classes:

  • equals() - The equals() function returns true if two objects have the identical contents. It operates similarly to \"==,\" although for Float and Double values it works differently.
  • hashCode() - The hashCode() function returns the object\'s hashcode value.
  • copy() - The copy() function is used to duplicate an object, changing only a few of its characteristics while leaving the rest unaltered.
  • toString() - This function returns a string containing all of the data class\'s parameters.

To ensure consistency, data classes must meet the following requirements:

  • At least one parameter is required for the primary constructor.
  • val or var must be used for all primary constructor parameters.
  • Abstract, open, sealed, or inner data classes are not possible.
  • Only interfaces may be implemented by data classes.

Example:

data class Sample(var input1 : Int, var input2 : Int)

The above code snippet creates a data class Sample with two parameters.

fun main(agrs: Array) {     
val temp = Sample(1, 2)
println(temp)

Here, we create an instance of the data class Sample and pass the parameters to it.

Output:-

Sample(input1=1, input2=2)

Is it helpful? Add Comment View Comments
 

Ques 8. Explain the concept of null safety in Kotlin.

Kotlin's type system aims to eradicate null references from the code. If a program throws NullPointerExceptions at runtime it might result in application failure or system crashes. If the Kotlin compiler finds a null reference it throws a NullPointerException.

The Kotlin type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). Null cannot be stored in a String variable. We get a compiler error if we try to assign null to the variable. 

var a: String = "interview"
a = null // results in compilation error

If we want the above string to be able to hold null value as well, we can declare it of type nullable using the ‘?’ operator after the String keyword as follows :

var a: String? = "interview"
a = null // no compilation error

Kotlin provides Safe Call (?.), Elvis (?:) and Not Null Assertion (!!) operators which define what needs to be done in case of a null encounter. This makes the code more reliable and less prone to errors. Thus, Kotlin enforces null safety by having nullable, non-nullable type variables and the different operators to tackle null encounters.

Is it helpful? Add Comment View Comments
 

Ques 9. Explain the various methods to iterate over any data structure in Kotlin with examples.

Following are the different ways to iterate over any data structure in Kotlin :

  • For Loop - The for loop is used to scan any data structure that supplies an iterator in this case. It is not used in the same way as the for loop in other programming languages such as Java or C.

In Kotlin, the for loop has the following Syntax:

for(item in collection) {     
// code
}

Here, collection refers to the data structure to be iterated and item refers to each element of the data structure.

For example,

// KOTLIN
fun main(args: Array) {
var numbersArray = arrayOf(1,2,3,4,5,6,7,8,9,10)
for (num in numbersArray){
if(num % 2 == 0){
print("$num ")
}
}
}

Output -

2 4 6 8 10
  • While Loop - It is made up of a code block and a condition to be checked for each iteration. First, the while condition is assessed, and if it is true, the code within the block is executed. Because the condition is verified every time before entering the block, it repeats until the condition turns false. The while loop can be thought of as a series of if statements that are repeated.

The while loop\'s syntax is as follows:

while(condition) {         
// code
}

For example,

// KOTLIN
fun main(args: Array) {
var number = 1
while(number <= 5) {
println(number)
number++;
}
}

Output -

12345
  • Do While Loop - The condition is assessed after all of the statements inside the block have been executed. If the do-while condition is true, the code block is re-executed. As long as the expression evaluates to true, the code block execution procedure is repeated. The loop ends if the expression becomes false, and control is passed to the sentence following the do-while loop. Because it verifies the condition after the block is executed, it\'s also known as a post-test loop.

The do-while loop\'s syntax is as follows:

do {     
// code
} while(condition)

For example,

// KOTLIN
fun main(args: Array) {
var number = 4
var sum = 0
do {
sum += number
number--
} while(number > 0)
println("Sum of first four natural numbers is $sum")
}

Output -

Sum of first four natural numbers is 10

Is it helpful? Add Comment View Comments
 

Ques 10. How can you concatenate two strings in Kotlin?

Following are the different ways by which we can concatenate two strings in Kotlin:

Using String Interpolation:- We use the technique of string interpolation to concatenate the two strings. Basically, we substitute the strings in place of their placeholders in the initialisation of the third string.

val s1 = "Without"
val s2 = "Book"
val s3 = "$s1 $s2" // stores "Without Book"

Using the + or plus() operator:- We use the ‘+’ operator to concatenate the two strings and store them in a third variable.

val s1 = "Without"
val s2 = "Book"
val s3 = s1 + s2 // stores "WithoutBook"
val s4 = s1.plus(s2) // stores "WithoutBook"

Using StringBuilder:- We concatenate two strings using the StringBuilder object. First, we append the first string and then the second string. 

val s1 = "Without"
val s2 = "Book"
val s3 =  StringBuilder()   
s3.append(s1).append(s2)
val s4 = s3.toString() // stores "WithoutBook"

Is it helpful? Add Comment View Comments
 

Ques 11. What’s the Target Platform of Kotlin? How is Kotlin-Java interoperability possible?

Java Virtual Machine(JVM) is the Target Platform of Kotlin. Kotlin is 100% interoperable with Java since both, on compilation produce bytecode. Hence Kotlin code can be called from Java and vice-versa.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related differences

Java vs Kotlin

Related interview subjects

Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
©2023 WithoutBook