Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Ruby - Operators and Expressions
Identify the error in this code snippet:
name = ""
name ||= "Guest"
puts name
ANo error; output is empty string
BSyntax error due to ||= usage
Cname becomes "Guest" incorrectly
DRuntime error because name is empty
Step-by-Step Solution
Solution:
  1. Step 1: Check initial value of name

    name is an empty string, which is truthy in Ruby.
  2. Step 2: Apply ||= operator

    Since name is truthy, name ||= "Guest" does not change name.
  3. Final Answer:

    No error; output is empty string -> Option A
  4. Quick Check:

    Empty string is truthy, ||= skips assignment [OK]
Quick Trick: Empty string is truthy, ||= won't assign new value [OK]
Common Mistakes:
MISTAKES
  • Thinking empty string is falsy
  • Expecting name to become "Guest"
  • Assuming syntax error with ||= operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes