Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
210 views

Kotlin Interview Questions

The document provides an overview of frequently asked Kotlin interview questions and answers. It discusses why the author switched to Kotlin from Java, key Kotlin features like extension functions and null safety, data classes, the Kotlin entry point, init blocks, primary vs secondary constructors, val vs var, and more. Links are provided for additional references.

Uploaded by

Ankur chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views

Kotlin Interview Questions

The document provides an overview of frequently asked Kotlin interview questions and answers. It discusses why the author switched to Kotlin from Java, key Kotlin features like extension functions and null safety, data classes, the Kotlin entry point, init blocks, primary vs secondary constructors, val vs var, and more. Links are provided for additional references.

Uploaded by

Ankur chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Kotlin Interview Questions

Frequently asked top Kotlin Interview Questions and Answers with detailed example programs and references
are provided here. Links would be referenced at appropriate junctures in the answers. You may refer those in
case you need any clarification. Sequential reading of questions and answers is suggested, as it simulates a
kind of interactive session.

Why did you switch to Kotlin from Java ?

Kotlin seems to be simpler and cleaner than Java. It removes a lot of redundancies in code from Java. Kotlin
also adds some needed features that Java doesn’t yet support, and is making code more idiomatic. Also Kotlin
has been added to Android Studio’s list of supported languages recently. So, there is much to expect from
Kotlin in easing out the development efforts and good support in future.

What are the features you think are there in Kotlin but not in Java ?

Kotlin has quite a number of features that Java doesn’t. To name some of them, they are

Extension Functions
Null Safety
Smart casts
Range expressions
Operator Overloading
Data classes
Companion Objects
Coroutines
etc.

What kinds of programming does Kotlin support ?

Kotlin supports two types of programming. They are

1. Procedural Programming
2. Object Oriented Programming

What is the entry point to a Kotlin program ? Provide an example.

Like most of the other procedural languages, main() function is the entry point to a Kotlin program.
An Example for main() function is

Kotlin Program – example.kt

fun main(args: Array<String>) {


val user1 = User(name="Yogi", age=27)
printUser(user1)
}

fun printUser(user: User){


println(user)
}

data class User(val name: String, val age: Int);

Reference – Kotlin main function

How do you think extension functions are useful ? – Kotlin Interview Question

Extension functions helps to extend a class with new functionality without having to inherit from the class. Also
you may use them like an inbuilt function for the class throughout the application.

Reference – Kotlin Extension Functions

What are Data classes ? Aren’t they available in Java ? – Kotlin Interview Question

Sometimes we use a class just to hold the data and nothing else. These classes are called Data classes. Of
course these kind of classes could be built using Java, but with explicit implementation of getter and setter for
each of the properties of class. Also you may need to implement functions like equals, toString and copy
separately. What Kotlin does is implementing all these automatically along with special functions called
component functions. How cool is that, removing the redundant code bloat.

Reference – Kotlin Data Class

Does Kotlin provide any additional functionalities for standard Java packages or standard
Java classes? – Kotlin Interview Question

Ofcourse, Yes. Kotlin uses the concept of extension functions, that we already talked about, to build some
useful and more widely used functions among developers directly into the Kotlin library.

Hmm! Where does this Kotlin run ? Does it have some kind of different runtime environment
?
Once compiled, Kotlin programs can run on standard JVM like some other compiled Java code. This means that
Kotlin Compiler compiles Kotlin programs to byte-code, which is understood by JVM. So, Kotlin is like a flavor
of Java, that goes alongside Java. Interesting fact is that, Kotlin applications can be built with parts of Java
code.

So, how do you migrate the code from Java to Kotlin ? – Kotlin Interview Question

JetBrains IDEA provides inbuilt tools to convert Java code to Kotlin code. Then you may do the magic offered
by Kotlin at some of the parts in code, to make it clean.

Reference – Convert Java File to Kotlin File

OK. Is there something called init block in Kotlin ?

Yes.

What does init block do and Where does it appear in a class ? – Kotlin Interview Question

Instructions in the init block are executed right after Primary Constructor’s execution. init block goes in a class
along with secondary constructors as a method.

Reference – Kotlin Init

How many types of constructors are there ? What are they ?

There are two types of constructors. They are Primary Constructors and Secondary Constructors.

How are Primary Constructors different from Secondary Constructors ?

Primary Constructors are declared intrinsically with class definition. Secondary Constructors are declared
exclusively inside the class body.

In the following example, in the first line, the constructor keyword along with the variables declared right after it
is the Primary Constructor. Inside the class body, we have another constructor, and this is Secondary
Constructor.

Kotlin Program – example.kt

class Person constructor(var name: String, var age: Int){


class Person constructor(var name: String, var age: Int){
var profession: String = "Not Mentioned"

constructor (name: String, age: Int, profession: String): this(name,age){


this.profession = profession
}
}

Is there any dependency of Secondary Constructors on Primary Constructors ?

Yes. Secondary Constructor has to make an exclusive call to Primary Constructor or other Secondary
Constructor, which of course calls the Primary Constructor. Following is an example, and here the Secondary
Constructor makes call to Primary Constructor using this(name, age) .

Kotlin Program – example.kt

class Person constructor(var name: String, var age: Int){


var profession: String = "Not Mentioned"

constructor (name: String, age: Int, profession: String): this(name,age){


this.profession = profession
}

fun printPersonDetails(){
println("$name whose profession is $profession, is $age years old.")
}
}

What is the difference between val and var ? – Kotlin Interview Question

Val (Value) is like a constant. Once assigned a value, you cannot change it. On the other hand Var (Variable) is
designed to be a storage location that can accept reassignment of values of same data type or what ever
feasible by the data type casting.

Reference – val vs var in Kotlin

What is Kotlin’s Null Safety ? – Kotlin Interview Question

Null Safety in Kotlin is to eliminate the risk of occurrence of NullPointerException in real time. Kotlin can
differentiate between nullable references and non-nullable references. If a variable has to be allowed to store a
null value, that has to be declared with a null (?) operator.

Reference – Null Safety in Kotlin

If you have worked with files, name some of the extension methods Kotlin provides to
java.io.File
java.io.File

Kotlin provides very useful extension functions to java.io.File. Some of them are :

File.bufferedReader() : to read contents of a file into BufferedReader


File.forEachLine() : to read a file line by line in Kotlin
File.inputStream() : to read contents of file to InputStream
File.readBytes() : to read contents of file to ByteArray
File.readLines() : to read lines in file to List
File.readText() : to read contents of file to a single String

For examples to these methods refer – Kotlin Read File Content

Is there Ternary Conditional Operator in Kotlin like in Java ?

No.

How do you realize Ternary Conditional Operator in Kotlin ?

A simple if else should do the job.

if (condition) a else b

How do you declare a variable as volatile in Kotlin ? – Kotlin Interview Question

By providing volatile annotation before the declaration of variable.

@Volatile var a: Long? = null

How do you check if two Strings are equal valued ? – Kotlin Interview Question

Using == (double equal to) operator.

Kotlin Program – example.kt

fun main(args: Array<String>) {


val a: String = "kotlin is easy"
val b: String = "kotlin is" + " easy"
if(a==b){
println(" a and b are equal.")
} else {
println(" a and b are not equal.")
}
}
}

Reference – Kotlin compare Strings

Kotlin Java

✦ Kotlin Tutorial

Getting Started

✦ Setup Kotlin(Java) Project

✦ Kotlin Example Program

✦ Convert Java to Kotlin

✦ Kotlin Main Function

✦ Kotlin Loops

✦ Kotlin For Loop

✦ Kotlin While, Do While Loops

✦ Kotlin Repeat

✦ Kotlin Ranges

✦ Kotlin When

Object Oriented Concepts

Classes

✦ Kotlin - Class, Primary and Secondary Constructors

✦ Kotlin Sealed Class

✦ Kotlin Data Class

✦ Kotlin Enum

✦ Kotlin - Extension Functions

Inheritance

✦ Kotlin Inheritance

✦ Kotlin Override Method of Super Class

Abstraction

✦ Kotlin Abstraction
✦ Kotlin Abstract Class

✦ Kotlin - Interfaces

✦ Kotlin Null Safety

Exception Handling

✦ Kotlin Try Catch

✦ Kotlin Throw Exception

✦ Kotlin Custom Exception

Fix Compilation Errors

✦ Kotlin - Variable must be initialized

✦ Kotlin - Primary Constructor call expected

✦ Kotlin - Null can not be a value of a non-null type String

✦ Kotlin - Cannot create an instance of an abstract class

Kotlin - String Operations

✦ Kotlin - Compare Strings

✦ Kotlin - Replace String

✦ Kotlin - Split String

✦ Kotlin - Split String to Lines

✦ Kotlin - String Capitalize

Kotlin - Functions

✦ Kotlin Function - Default Arguments

✦ Kotlin - Use Function

Kotlin Collections

Kotlin List

✦ Kotlin List

✦ Kotlin List forEach

Kotlin File Operations

✦ Kotlin - Create File

✦ Kotlin - Read File


✦ Kotlin - Read File

✦ Kotlin - Read File as List of Lines

✦ Kotlin - Write to File

✦ Kotlin - Append Text to File

✦ Kotlin - Check if File Exists

✦ Kotlin - Copy a File to Other

✦ Kotlin - Iterate through all files in a directory

✦ Kotlin - Delete Recursively

✦ Kotlin - Get File Extension

Kotlin Interview Q/A

➩ Kotlin Interview Questions

Kotlin Android

✦ Kotlin Android Tutorial

Useful Resources

✦ How to Learn Programming

You might also like