menu

Kotlin Variables and Data Types

Kotlin Variables and Data Types - Important Points


16. Which of the following is a valid way to declare a variable in Kotlin with a default value?

A. var count = 0

B. var count: Int

C. var count: Int = null

D. var count = null

Discuss Work Space

Answer: option a

Explanation:

Option A is a valid variable declaration with a default value of 0. In Kotlin, you can also declare variables with default values using the constructor of a class.

17. Which of the following is a valid Kotlin variable declaration for a set of integers?

A. val numbers = hashSetOf(1, 2, 3)

B. val numbers: Set = [1, 2, 3]

C. val numbers: MutableSet = setOf(1, 2, 3)

D. val numbers: Set = setOf(1, 2, 3)

Discuss Work Space

Answer: option d

Explanation:

In Kotlin, you can declare a set using the "setOf()" function. Option D is a valid variable declaration for a set of integers named "numbers".

18. Which of the following is a valid way to declare a variable in Kotlin with a constant value?

A. var PI: Float = 3.14f

B. val PI = 3.14f

C. const val PI = 3.14f

D. final val PI = 3.14f

Discuss Work Space

Answer: option c

Explanation:

In Kotlin, you can declare a variable with a constant value using the "const" keyworOption C is a valid way to declare a constant variable named "PI" with a value of 3.14f.

19. Which of the following is a valid way to declare a variable in Kotlin with a lazy initialization?

A. val name by lazy = "John"

B. var name = lazy { "John" }

C. lateinit var name: String = "John"

D. val name: String? = null

Discuss Work Space

Answer: option a

Explanation:

In Kotlin, you can declare a variable with lazy initialization using the "lazy" keyworOption A is a valid way to declare a variable named "name" with lazy initialization and an initial value of "John".

20. Which of the following is a valid way to declare a nullable variable in Kotlin?

A. var name: String = null

B. var name: String? = null

C. var name: Nullable = null

D. var name: NullableString = null

Discuss Work Space

Answer: option b

Explanation:

In Kotlin, you can declare a variable as nullable by appending a "?" to the type. Option B is a valid way to declare a nullable string variable named "name" with an initial value of null.


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