Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q6 of 15
Kotlin - Data Types
Identify the error in this Kotlin code snippet:
val text = "Hello"
text[0] = 'h'
println(text)
AIndex out of bounds error
BMissing semicolon after assignment
CCannot assign to a String character; Strings are immutable
DVariable text is not declared
Step-by-Step Solution
Solution:
  1. Step 1: Understand String immutability in Kotlin

    Strings cannot be changed character by character; they are immutable.
  2. Step 2: Analyze the assignment to text[0]

    Assigning to a character index causes a compile-time error because String characters cannot be changed.
  3. Final Answer:

    Cannot assign to a String character; Strings are immutable -> Option C
  4. Quick Check:

    Strings are immutable; character assignment not allowed [OK]
Quick Trick: You cannot change characters inside a String directly [OK]
Common Mistakes:
MISTAKES
  • Trying to assign to String index
  • Assuming Strings behave like arrays
  • Ignoring immutability rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes