Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Variables and Type System
Identify the error in this Kotlin code snippet:
var name: String
name = "Alice"
name = 123
AVariable name must be declared with val
BCannot assign a number to a String variable
CMissing initial value for var
DSyntax error in variable declaration
Step-by-Step Solution
Solution:
  1. Step 1: Check variable type and assignments

    Variable name is declared as String, assigned "Alice" (valid), then assigned 123 (number).
  2. Step 2: Identify type mismatch error

    Assigning an integer to a String variable causes a type error.
  3. Final Answer:

    Cannot assign a number to a String variable -> Option B
  4. Quick Check:

    Type mismatch causes error [OK]
Quick Trick: Check variable type before assigning new value [OK]
Common Mistakes:
MISTAKES
  • Ignoring type mismatch errors
  • Thinking var requires initial value always
  • Confusing val and var usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes