menu

Kotlin Variables and Data Types


1. Which of the following is a valid way to declare an array in Kotlin?

val numbers = [1, 2, 3]

val numbers = arrayOf(1, 2, 3)

val numbers: Array = [1, 2, 3]

val numbers: Array = arrayOf(1, 2, 3)


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

Char

String

Int

Float


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


4. Which of the following data types in Kotlin is used to represent floating-point numbers?

Int

Long

Float

Double


5.

What is the output of the following Kotlin code?

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

null

0

Compilation error

Runtime error


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


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

var name = "John"

var 1name = "John"

var name = 1

var name: String = "John"


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

255

32767

2147483647

9.22337203685477e18