Bird
0
0

Find the error in this Ruby code snippet:

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

Find the error in this Ruby code snippet:

sym = :hello
sym << " world"
puts sym
ASymbols can be changed with << operator
BSymbols cannot be concatenated with <<
CSyntax error in symbol declaration
DMissing quotes around symbol
Step-by-Step Solution
Solution:
  1. Step 1: Check symbol mutability

    Symbols are immutable, so you cannot change them or add to them with operators like <<.
  2. Step 2: Identify the error in code

    The line sym << " world" tries to modify the symbol, which causes a runtime error.
  3. Final Answer:

    Symbols cannot be concatenated with << -> Option B
  4. Quick Check:

    Symbols are immutable, no << allowed [OK]
Quick Trick: Symbols cannot be changed or concatenated [OK]
Common Mistakes:
  • Trying to modify symbols like strings
  • Assuming symbols can be concatenated
  • Confusing syntax errors with runtime errors
  • Using << operator on immutable objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes