Kotlin Interview Questions and Answers for Android App Developer (2022)

Kotlin Interview Questions and Answers for Freshers and Experienced (2022)

Kotlin Interview Questions for Freshers

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

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

var website = “GBAndroidBlogs”

val variableName = value

var variableName = value

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

data class className ( list_of_parameters)

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

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

4. Explain the concept of null safety in Kotlin.

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

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

5. Explain Safe call, Elvis, and Not Null Assertion operator in the context of Kotlin.

name?.toLowerCase()

if(name != null)
name.toLowerCase()
else
null

val sample1 = sample2 ?: “Undefined”

val sample1 = if(sample2 != null)
sample2
else
“Undefined”

// KOTLIN
fun main(args: Array<String>) {
var sample : String? = null
str!!.length
}

Exception in thread “main” kotlin.KotlinNullPointerException

6. Differentiate between Kotlin and Java.

7. What are the different types of constructors available in Kotlin? Explain them with proper examples.

class Sample constructor(val a: Int, val b: Int) {
// code
}

// KOTLIN
fun main(args: Array<String>) {
val s1 = Sample(1, 2)
}
class Sample(a : Int , b: Int) {
val p: Int
var q: Int
// initializer block
init {
p = a
q = b
println(“The first parameter value is : $p”)
println(“The second parameter value is : $q”)
}
}

// KOTLIN
fun main(args: Array<String>) {
val s1 = Sample(1, 2)
}
class Sample {
constructor(a: Int, b: Int) {
println(“The first parameter value is : $p”)
println(“The second parameter value is : $q”)
}
}

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

for(item in collection) {
// code
}

// KOTLIN
fun main(args: Array<String>) {
var numbersArray = arrayOf(1,2,3,4,5,6,7,8,9,10)

for (num in numbersArray){
if(num % 2 == 0){
print(“$num “)
}
}
}

while(condition) {
// code
}

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

do {
// code
{
while(condition)

// KOTLIN
fun main(args: Array<String>) {
var number = 4
var sum = 0

do {
sum += number
number —
}while(number > 0)
println(“Sum of first four natural numbers is $sum”)
}

9. How can you concatenate two strings in Kotlin?

val s1 = “Interview”
val s2 = “Bit”
val s3 = “$s1 $s2” // stores “Interview Bit”

val s1 = “Interview”
val s2 = “Bit”
val s3 = s1 + s2 // stores “InterviewBit”
val s4 = s1.plus(s2) // stores “InterviewBit”

val s1 = “Interview”
val s2 = “Bit”
val s3 = StringBuilder()
s3.append(s1).append(s2)
val s4 = s3.toString() // stores “InterviewBit”

10. What do you understand about function extension in the context of Kotlin? Explain.

// KOTLIN
class Sample {
var str : String = “null”

fun printStr() {
print(str)
}
}
fun main(args: Array<String>) {
var a = Sample()
a.str = “Interview”
var b = Sample()
b.str = “Bit”
var c = Sample()
c.str = a.add(b)
c.printStr()
}
// function extension
fun Sample.add(a : Sample):String{
var temp = Sample()
temp.str = this.str + “ “ +a.str
return temp.str
}

11. What do you understand about Companion Object in the context of Kotlin?

// Syntax in KOTLIN
class CompanionClass {
companion object CompanionObjectName {
// code
}
}
val obj = CompanionClass.CompanionObjectName

// KOTLIN
class CompanionClass {
companion object {
// code
}
}
val obj = CompanionClass.Companion

12. Differentiate between open and public keywords in Kotlin.

13. Explain about the “when” keyword in the context of Kotlin.

// KOTLIN
fun main(args: Array<String>) {

var temp = “Interview”
when(temp) {
“Interview” -> println(“Interview Bit is the solution.”)
“Job” -> println(“Interview is the solution.”)
“Success” -> println(“Hard Work is the solution.”)
}
}

14. What are the advantages of Kotlin over Java?

Kotlin Interview Questions for Experienced

15. Which one is better to use — val mutableList or var immutableList in the context of Kotlin?

16. What do you understand about lateinit in Kotlin? When would you consider using it?

// KOTLIN
lateinit var test: String
fun testFunction() {
test = “Interview”
println(“The length of string is “+test.length)
test = “Bit”
}

17. Explain lazy initialization in the context of Kotlin.

// KOTLIN
class FastClass {
private val slowObject: SlowClass = SlowClass()
}

// KOTLIN
class FastClass {
private val slowObject: SlowClass by lazy {
println(“Slow Object initialised”)
SlowClass()
}

fun access() {
println(slowObject)
}
}
fun main(args: Array<String>) {
val fastClass = FastClass()
println(“FastClass initialised”)
fastClass.access()
fastClass.access()
}

18. Differentiate between lateinit and lazy initialisation. Explain the cases when you should use lateinit and when you should use lazy initialisation.

19. What do you understand about coroutines in the context of Kotlin?

20. Explain scope functions in the context of Kotlin. What are the different types of Scope functions available in Kotlin?

21. Explain suspend function in the context of Kotlin.

22. What do you understand about sealed classes in Kotlin?

sealed class className

// KOTLIN
sealed class Sample {
class A : Sample() {
fun print()
{
println(“This is the subclass A of sealed class Sample”)
}
}
class B : Sample() {
fun print()
{
println(“This is the subclass B of sealed class Sample”)
}
}
}
fun main()
{
val obj1 = Sample.B()
obj1.print()

val obj2 = Sample.A()
obj2.print()
}

23. What do you understand about the backing field in Kotlin?

var marks: Int = someValue
get() = field
set(value) {
field = value
}

24. Differentiate between launch / join and async / await in Kotlin.

25. What are some of the disadvantages of Kotlin?

Test Yourself:

--

--

Hi everyone, myself Golap an Android app developer with UI/UX designer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Golap Gunjan Barman

Hi everyone, myself Golap an Android app developer with UI/UX designer.