Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Classes and Objects
What will be the output of this Ruby code?
x = "code"
y = "code"
puts x.equal?(y)
puts x == y
Afalse true
Btrue true
Cfalse false
Dtrue false
Step-by-Step Solution
Solution:
  1. Step 1: Analyze equal? call

    Since x and y are two distinct string objects with the same content, x.equal?(y) returns false.
  2. Step 2: Analyze == call

    == compares content, so x == y returns true.
  3. Final Answer:

    false true -> Option A
  4. Quick Check:

    Different objects with same content: equal? false, == true [OK]
Quick Trick: equal? false if different objects, == true if content matches [OK]
Common Mistakes:
  • Assuming equal? returns true for identical strings
  • Confusing == with equal?
  • Expecting both outputs to be true

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes