menu

Kotlin Basics


1.

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.


2. What is the syntax for a lambda expression in Kotlin?

{x, y -> x + y}

(x, y) => x + y

[x, y] -> x + y

(x, y) -> {x + y}


3. What is the primary use case of Kotlin?

Mobile app development

Web development

Database management

Machine learning


4.

What is the output of the following code?

fun main() {
val x = 5
when (x) {
1 -> println("x is 1")
2 -> println("x is 2")
else -> println("x is not 1 or 2")
}
}

A. x is 1

x is 2

x is not 1 or 2

This code will not compile.


5.

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.


6. 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.


7.

What is Kotlin?

A programming language

A database management system

A web framework

A markup language


8.

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


9. 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.


10. Which of the following is true about extension functions in Kotlin?

Extension functions are a way to add new functionality to existing classes without having to modify their source code.

Extension functions can only be defined for classes that are part of the Kotlin standard library.

Extension functions are not supported in Kotlin.

Extension functions can only be defined for classes that are marked as "open".