menu

Kotlin Variables and Data Types


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


2.

What is the output of the following Kotlin code?

fun main() {
var num = 5
num = num + 2
println(num)
}

5

7

num + 2

5 + 2


3. Which of the following is a valid Kotlin variable name?

myVariableName

MyVariableName

my-variable-name

1myvariablename


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

Char

String

Int

Float


5. What is the default value of an uninitialized Kotlin variable of type Int?

null

0

0

0


6. Which of the following is a valid Kotlin variable declaration for a set of integers?

val numbers = hashSetOf(1, 2, 3)

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

val numbers: MutableSet = setOf(1, 2, 3)

val numbers: Set = setOf(1, 2, 3)


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


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

255

32767

2147483647

9.22337203685477e18


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

Int

Long

Float

Double


10.

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