menu

Kotlin Variables and Data Types

Kotlin Variables and Data Types - Important Points


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

A. val numbers = [1, 2, 3]

B. val numbers = arrayOf(1, 2, 3)

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

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

Discuss Work Space

Answer: option b

Explanation:

In Kotlin, you can declare an array using the "arrayOf()" function. Option B is a valid way to declare an array of integers named "numbers" with values [1, 2, 3].

22.

What is the output of the following Kotlin code?

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

A. null

B. 0

C. Compilation error

D. Runtime error

Discuss Work Space

Answer: option a

Explanation:

The output of the code snippet is null because the safe call operator "?" is used to access the "length" property of the nullable string "name", which is null.

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

A. var count: Int! = null

B. var count: Int = null!!

C. var count: Int? = null

D. var count: Int!! = null

Discuss Work Space

Answer: option a

Explanation:

In Kotlin, you can declare a variable with a non-null assertion using the "!" operator. Option A is a valid way to declare a nullable integer variable named "count" with a non-null assertion.


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