Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
Ruby - String Operations
What is wrong with this code snippet?
str = "Hi"
str << nil
puts str
Anil is converted to "nil" string automatically.
BYou cannot append nil using << operator; it causes an error.
CThe code will output "Hi" without changes.
DThe << operator ignores nil values silently.
Step-by-Step Solution
Solution:
  1. Step 1: Understand << operator argument requirements

    The << operator requires a string argument; nil is not a string.
  2. Step 2: Check behavior when appending nil

    Appending nil raises a TypeError because nil cannot be converted to string automatically.
  3. Final Answer:

    You cannot append nil using << operator; it causes an error. -> Option B
  4. Quick Check:

    Appending nil with << causes error [OK]
Quick Trick: Never append nil directly; convert or check first [OK]
Common Mistakes:
  • Expecting nil to convert automatically
  • Ignoring type errors
  • Assuming << skips nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes