Bird
0
0

What is wrong with this Ruby code snippet?

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

What is wrong with this Ruby code snippet?

sym = :greeting
sym += "!"
AYou must convert the symbol to a string before concatenation.
BThe += operator is not defined for symbols.
CSymbols are immutable; you cannot concatenate strings to them.
DSymbols cannot be assigned to variables.
Step-by-Step Solution
Solution:
  1. Step 1: Understand symbol immutability

    Ruby symbols are immutable, meaning their value cannot be changed once created.
  2. Step 2: Analyze the operation

    The code attempts to concatenate a string to a symbol using +=, which is not allowed because symbols cannot be modified.
  3. Final Answer:

    Symbols are immutable; you cannot concatenate strings to them. -> Option C
  4. Quick Check:

    Attempting to modify a symbol raises an error. [OK]
Quick Trick: Symbols cannot be changed after creation. [OK]
Common Mistakes:
MISTAKES
  • Thinking symbols behave like strings and can be concatenated.
  • Assuming += works on symbols like on strings.
  • Not realizing symbols are immutable objects.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes