Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Classes and Objects
What will be the output of the following Ruby code?
a = "hello"
b = "hello"
puts a == b
puts a.equal?(b)
Atrue true
Btrue false
Cfalse true
Dfalse false
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate a == b

    Both a and b are strings with the same content "hello", so a == b returns true.
  2. Step 2: Evaluate a.equal?(b)

    Though content is same, a and b are different string objects, so equal? returns false.
  3. Final Answer:

    true false -> Option B
  4. Quick Check:

    == compares content, equal? compares object identity = C [OK]
Quick Trick: == checks value, equal? checks same object [OK]
Common Mistakes:
  • Assuming equal? returns true for identical strings
  • Confusing output order
  • Thinking == checks object identity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes