Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Inheritance
What will be the output of this Ruby code?
class Animal; end
class Dog < Animal; end
class Cat; end
puts Dog < Animal
puts Cat < Animal
Atrue false
Bfalse true
Ctrue true
Dfalse false
Step-by-Step Solution
Solution:
  1. Step 1: Understand the < operator for classes

    In Ruby, ChildClass < ParentClass returns true if ChildClass is a subclass of ParentClass, otherwise false.
  2. Step 2: Analyze the code

    Dog < Animal returns true because Dog inherits from Animal. Cat < Animal returns false because Cat does not inherit from Animal.
  3. Final Answer:

    true false -> Option A
  4. Quick Check:

    Dog is subclass of Animal [OK]
Quick Trick: Subclass check with < returns true only if inheritance exists [OK]
Common Mistakes:
  • Assuming unrelated classes return true
  • Confusing < with other comparison operators
  • Ignoring case sensitivity in class names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes