menu

Kotlin Basics


1. What is the primary use case of Kotlin?

Mobile app development

Web development

Database management

Machine learning


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


3.

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.


4.

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.


5. Which of the following is true about nullable types in Kotlin?

All types in Kotlin are nullable by default.

Only reference types in Kotlin can be nullable.

All types in Kotlin are non-nullable by default.

Nullable types cannot be used in Kotlin.


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 syntax for a lambda expression in Kotlin?

{x, y -> x + y}

(x, y) => x + y

[x, y] -> x + y

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


8. 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".


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

var

my-variable

123foo

MyClass


10. What is the difference between a range and an array in Kotlin?

A range is a collection of values between two endpoints, while an array is a fixed-size collection of elements.

A range is a fixed-size collection of elements, while an array is a variable-size collection of elements.

There is no difference between a range and an array in Kotlin.

A range and an array are both used to store collections of values.