menu

Kotlin Variables and Data Types


1.

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


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


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 data types in Kotlin is used to represent characters?

Char

String

Int

Float


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

Char

String

Int

Boolean


6. Which of the following is a valid way to declare a variable in Kotlin with a non-null assertion?

var count: Int! = null

var count: Int = null!!

var count: Int? = null

var count: Int!! = null


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


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


9.

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


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