menu

Kotlin Variables and Data Types


1. Which of the following is a valid Kotlin variable declaration for a nullable String?

var name: String = null

var name: String? = null

var name = null

var name: Nullable = null


2.

What is the output of the following Kotlin code?

fun main() {
val a = 5
val b = 2
val result = a.toDouble() / b
println(result)
}

2.5

2

2

2.5f


3. Which of the following data types in Kotlin is used to represent true or false values?

Char

String

Int

Boolean


4.

What is the output of the following Kotlin code?

fun main() {
val name: String? = null
println(name?.length)
}

null

0

Compilation error

Runtime error


5. What is the maximum value that can be stored in a Kotlin variable of type Short?

255

32767

2147483647

9.22337203685477e18


6.

What is the output of the following Kotlin code?

fun main() {
val name = "John"
println("Hello, $name!")
}

Hello, $name!

Hello, John!

Hello, {name}!

Hello, {John}!


7. Which of the following is a valid way to declare a constant variable in Kotlin?

const val name = "John"

var name = "John"

val name = "John"

final val name = "John"


8. Which of the following is a valid way to declare a variable in Kotlin with a lazy initialization?

val name by lazy = "John"

var name = lazy { "John" }

lateinit var name: String = "John"

val name: String? = null


9.

What is the output of the following Kotlin code?

fun main() {
val a = 5
val b = 2
val result = a / b
println(result)
}

2.5

2

2

2.5f


10. Which of the following is a valid way to declare a nullable variable in Kotlin?

var name: String = null

var name: String? = null

var name: Nullable = null

var name: NullableString = null