menu

Kotlin Variables and Data Types


1.

What is the output of the following Kotlin code?

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

null

0

Compilation error

Runtime error


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

var name = "John"

var 1name = "John"

var name = 1

var name: String = "John"


3. Which of the following is a valid Kotlin variable declaration for a list of strings?

val names: ArrayList = ["John", "Jane"]

val names = ["John", "Jane"]

val names: List = listOf("John", "Jane")

val names = arrayListOf("John", "Jane")


4.

What is the output of the following Kotlin code?

fun main() {
val x = 5
val y = "10"
val result = x + y.toInt()
println(result)
}

510

15

510

15


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

255

32767

2147483647

9.22337203685477e18


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

var PI: Float = 3.14f

val PI = 3.14f

const val PI = 3.14f

final val PI = 3.14f


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

var count = 0

var count: Int

var count: Int = null

var count = null


8.

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}!


9. Which of the following data types in Kotlin is used to represent characters?

Char

String

Int

Float


10.

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