Bird
0
0

Identify the error in the following Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - String Operations
Identify the error in the following Ruby code snippet:
text = "Hello"
text +<< " World"
puts text
ANo error; output is "Hello World"
BSyntaxError due to incorrect use of <code>+<<</code>
CRuntimeError because <code>text</code> is immutable
DOutput is "Hello" because <code>+</code> does not modify original
Step-by-Step Solution
Solution:
  1. Step 1: Check the operator usage

    The code uses +<< together, which is not a valid Ruby operator or syntax.
  2. Step 2: Understand Ruby syntax rules

    Ruby does not allow chaining + and << without spaces or parentheses; this causes a syntax error.
  3. Final Answer:

    SyntaxError due to incorrect use of +<< -> Option B
  4. Quick Check:

    Invalid operator +<< causes syntax error [OK]
Quick Trick: Operators + and << cannot be combined directly [OK]
Common Mistakes:
  • Assuming +<< is valid syntax
  • Expecting + to modify original string
  • Ignoring syntax errors from invalid operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes