menu

Kotlin Basics


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


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

fun myFunction()

fun myFunction(): String

fun myFunction(): Int = 42

All of the above


3.

What is Kotlin?

A programming language

A database management system

A web framework

A markup language


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 primary use case of Kotlin?

Mobile app development

Web development

Database management

Machine learning


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


7.

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.


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

var

my-variable

123foo

MyClass


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