Bird
0
0

What will be the output of the following Ruby code?

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

What will be the output of the following Ruby code?

sym1 = :apple
sym2 = :apple
puts sym1.object_id == sym2.object_id
Afalse
Bnil
Ctrue
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand symbol object identity

    Symbols with the same name share the same object_id because they are unique and immutable.
  2. Step 2: Compare object_ids of sym1 and sym2

    Both :apple symbols point to the same object, so their object_ids are equal, making the comparison true.
  3. Final Answer:

    true -> Option C
  4. Quick Check:

    Same symbol = same object_id [OK]
Quick Trick: Same symbol names share object_id, so comparison is true [OK]
Common Mistakes:
  • Assuming different symbols have different object_ids
  • Confusing symbols with strings which differ
  • Expecting false because of separate variables
  • Thinking symbols are mutable objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes