Which of the following Kotlin code snippets correctly declares an immutable String variable?
easy📝 Syntax Q3 of 15
Kotlin - Data Types
Which of the following Kotlin code snippets correctly declares an immutable String variable?
Aval name: String = "Alice"
Bvar name = 123
Cval name = 123
Dvar name: String = "Alice"
Step-by-Step Solution
Solution:
Step 1: Understand Kotlin variable declarations
val declares an immutable variable, var declares mutable. The type String must match the value.
Step 2: Check each option for immutability and type correctness
val name: String = "Alice" declares an immutable String correctly. var name: String = "Alice" is mutable. Options B and C assign Int values, not String.
Final Answer:
val name: String = "Alice" -> Option A
Quick Check:
Use val for immutable String variables [OK]
Quick Trick:Use val for immutable variables, var for mutable [OK]
Common Mistakes:
MISTAKES
Confusing val and var keywords
Assigning wrong type to variable
Assuming var means immutable
Master "Data Types" in Kotlin
9 interactive learning modes - each teaches the same concept differently