Bird
0
0

You have these Ruby classes:

hard📝 Application Q15 of 15
Ruby - Inheritance
You have these Ruby classes:
class Animal; end
class Mammal < Animal; end
class Dog < Mammal; end
class Fish < Animal; end

puts Dog < Animal
puts Fish < Mammal
puts Mammal < Dog

What is the output and why?
Atrue false false
Btrue true false
Cfalse false true
Dtrue false true
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate Dog < Animal

    Dog inherits from Mammal, which inherits from Animal, so Dog is a subclass of Animal. Returns true.
  2. Step 2: Evaluate Fish < Mammal

    Fish inherits directly from Animal, not Mammal. So Fish is not a subclass of Mammal. Returns false.
  3. Step 3: Evaluate Mammal < Dog

    Mammal is a superclass of Dog, not a subclass. So this returns false.
  4. Final Answer:

    true false false -> Option A
  5. Quick Check:

    Subclass chain true, unrelated or reversed false [OK]
Quick Trick: Subclass < superclass = true; reversed or unrelated = false [OK]
Common Mistakes:
  • Assuming superclass < subclass returns true
  • Ignoring multi-level inheritance
  • Mixing up class hierarchy direction

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes