menu

Kotlin Basics


1. What is a coroutine in Kotlin?

A type of function that can be suspended and resumed later.

A type of loop that runs in the background.

A type of class that is used for creating objects.

A type of variable that can be shared between threads.


2. What is the difference between a class and an object in Kotlin?

A class is a blueprint for creating objects, while an object is a singleton instance of a class.

A class is a singleton instance of an object, while an object is a blueprint for creating classes.

There is no difference between a class and an object in Kotlin.

A class and an object are both used to create instances of a class.


3. What is the difference between val and var in Kotlin?

val is immutable while var is mutable.

var is immutable while val is mutable.

Both val and var are immutable.

Both val and var are mutable.


4. What is the Elvis operator in Kotlin?

A type of operator used for bitwise operations.

A type of operator used for arithmetic operations.

A type of operator used for null safety.

A type of operator used for logical operations.


5. What is the primary use case of Kotlin?

Mobile app development

Web development

Database management

Machine learning


6.

What is the output of the following code?

fun main() {
val numbers = mutableListOf(1, 2, 3, 4, 5)
numbers.removeIf { it % 2 == 0 }
println(numbers)
}

[1, 2, 3, 4, 5]

[1, 3, 5]

[2, 4]

This code will not compile.


7.

What is the output of the following code?

fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val filteredNumbers = numbers.filter { it % 2 == 0 }
println(filteredNumbers)
}

[1, 3, 5]

[2, 4]

[1, 2, 3, 4, 5]

This code will not compile.


8. Which of the following is an example of a string interpolation in Kotlin?

The answer is + answer

The answer is $answer

The answer is {answer}

The answer is [answer]


9.

What is the output of the following code?

fun main() {
val x = 5
val y = 10
val result = if (x > y) "x is greater than y" else "y is greater than x"
println(result)
}

A. x is greater than y

y is greater than x

5

This code will not compile.


10.

What is the output of the following code?

fun main() {
val nullableString: String? = null
val length = nullableString?.length ?: -1
println(length)
}

0

-1

null

This