Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Data Types
Identify the error in this Kotlin code snippet:
var greeting = "Hi"
greeting[0] = 'B'
println(greeting)
Aprintln cannot print Strings.
BVariable greeting should be val, not var.
CSingle quotes cannot be used for characters.
DYou cannot change characters in a String by index.
Step-by-Step Solution
Solution:
  1. Step 1: Check String mutability rules

    Kotlin Strings are immutable; you cannot assign to an index.
  2. Step 2: Analyze the code line causing error

    Line greeting[0] = 'B' tries to change a character, which is invalid.
  3. Final Answer:

    You cannot change characters in a String by index. -> Option D
  4. Quick Check:

    Strings immutable = no index assignment [OK]
Quick Trick: Strings are like frozen text, can't change letters inside [OK]
Common Mistakes:
MISTAKES
  • Thinking var allows character change inside String
  • Confusing char literals with strings
  • Believing println can't print Strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes