Bird
0
0

What is the main difference between var and val in Kotlin?

easy📝 Conceptual Q1 of 15
Kotlin - Variables and Type System
What is the main difference between var and val in Kotlin?
A<code>var</code> allows variable reassignment, <code>val</code> does not
B<code>val</code> allows variable reassignment, <code>var</code> does not
C<code>var</code> is used for constants, <code>val</code> for variables
DBoth <code>var</code> and <code>val</code> behave the same
Step-by-Step Solution
Solution:
  1. Step 1: Understand var and val usage

    var declares a mutable variable, meaning its value can change after initialization. val declares an immutable variable, which cannot be reassigned once set.
  2. Step 2: Compare mutability

    Since var allows reassignment and val does not, the main difference is mutability.
  3. Final Answer:

    var allows variable reassignment, val does not -> Option A
  4. Quick Check:

    Mutability = B [OK]
Quick Trick: Remember: var = changeable, val = fixed [OK]
Common Mistakes:
MISTAKES
  • Confusing var and val mutability
  • Thinking val variables can be reassigned
  • Assuming var is for constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes