menu

Kotlin Variables and Data Types

Kotlin Variables and Data Types - Important Points


11. Which of the following is a valid Kotlin variable declaration for a nullable String?

A. var name: String = null

B. var name: String? = null

C. var name = null

D. var name: Nullable = null

Discuss Work Space

Answer: option b

Explanation:

To declare a nullable variable in Kotlin, you can add a "?" after the data type. Option B is a valid variable declaration for a nullable String.

12.

What is the output of the following Kotlin code?

fun main() {
val a = 5
val b = 2
val result = a / b
println(result)
}

A. 2.5

B. 2

C. 2

D. 2.5f

Discuss Work Space

Answer: option b

Explanation:

The "/" operator performs integer division when used with two integers. So, the output of the code snippet is 2.

13.

What is the output of the following Kotlin code?

fun main() {
val a = 5
val b = 2
val result = a.toDouble() / b
println(result)
}

A. 2.5

B. 2

C. 2

D. 2.5f

Discuss Work Space

Answer: option a

Explanation:

The "toDouble()" function converts the integer value of "a" to a double value before performing division. So, the output of the code snippet is 2.5.

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

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

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

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

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

Discuss Work Space

Answer: option c

Explanation:

In Kotlin, you can declare a list using the "listOf()" function. Option C is a valid variable declaration for a list of strings named "names".

15.

What is the output of the following Kotlin code?

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

A. 510

B. 15

C. 510

D. 15

Discuss Work Space

Answer: option b

Explanation:

The "toInt()" function converts the string value of "y" to an integer before performing addition. So,


Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now