Bird
0
0

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:
  1. Step 1: Understand Kotlin variable declarations

    val declares an immutable variable, var declares mutable. The type String must match the value.
  2. 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.
  3. Final Answer:

    val name: String = "Alice" -> Option A
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes