menu

Kotlin Variables and Data Types


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


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


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

var name = "John"

var 1name = "John"

var name = 1

var name: String = "John"


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

myVariableName

MyVariableName

my-variable-name

1myvariablename


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


6.

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


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

Int

Long

Float

Double


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. Which of the following data types in Kotlin is used to represent true or false values?

Char

String

Int

Boolean


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

255

32767

2147483647

9.22337203685477e18