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
D. val numbers: Array
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() { |
---|
A. null
B. 0
C. Compilation error
D. Runtime error
View Answer Discuss Work SpaceAnswer: 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
View Answer Discuss Work SpaceAnswer: 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.