Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Variables and Data Types

Identify the error in this Ruby code:

sym = :hello
sym[0] = 'H'
ASymbols cannot be indexed or modified.
BSyntax error due to missing quotes.
CNo error; symbol is mutable.
DError because 'H' is not a symbol.
Step-by-Step Solution
Solution:
  1. Step 1: Understand symbol immutability

    Symbols are immutable and do not support indexing or modification.
  2. Step 2: Analyze code behavior

    Trying to assign to sym[0] causes an error because symbols cannot be changed.
  3. Final Answer:

    Symbols cannot be indexed or modified. -> Option A
  4. Quick Check:

    Symbol immutability error = A [OK]
Quick Trick: Symbols cannot be changed or indexed like strings [OK]
Common Mistakes:
  • Trying to modify symbols like strings
  • Assuming symbols support indexing
  • Confusing symbol immutability with string mutability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes