Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Variables and Data Types

What will be the output of this Ruby code?

sym1 = :cat
sym2 = :cat
puts sym1.object_id == sym2.object_id
Afalse
Btrue
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand symbol object IDs

    Symbols with the same name share the same object ID because they are unique and immutable.
  2. Step 2: Compare object IDs

    sym1 and sym2 both refer to :cat, so their object IDs are equal, outputting true.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    Symbol object_id equality = A [OK]
Quick Trick: Same symbol names share the same object ID [OK]
Common Mistakes:
  • Assuming different symbols have different object IDs
  • Confusing symbols with strings
  • Expecting false for identical symbols

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes