menu

Kotlin Basics


1.

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.


2. Which of the following is true about higher-order functions in Kotlin?

Higher-order functions take other functions as parameters or return functions as results.

Higher-order functions are only used for mathematical calculations.

Higher-order functions are not supported in Kotlin.

Higher-order functions are used to create singleton objects.


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

val is used for variables that can be changed, while "var" is used for variables that are constant.

val is used for variables that are constant, while "var" is used for variables that can be changed.

val and "var" are interchangeable and can be used interchangeably.

val and "var" are not supported in Kotlin.


4.

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


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


6. What is the difference between a primary constructor and a secondary constructor in Kotlin?

A primary constructor is called before any secondary constructors, while a secondary constructor is called before the primary constructor.

A primary constructor is defined inside the class definition, while a secondary constructor is defined outside the class definition.

A primary constructor takes parameters that are used to initialize properties, while a secondary constructor can have additional logic or functionality.

There is no difference between a primary and a secondary constructor in Kotlin.


7. What is the Elvis operator in Kotlin?

??

::

!!

//


8.

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.


9. Which of the following is a valid function declaration in Kotlin?

fun myFunction()

fun myFunction(): String

fun myFunction(): Int = 42

All of the above


10. 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}