Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Ruby - Inheritance
Consider this code:
class Vehicle; end
class Car < Vehicle; end
class SportsCar < Car; end
obj = SportsCar.new
puts obj.is_a?(Car) && obj.kind_of?(Vehicle)
What is the output?
AError
Btrue
Cnil
Dfalse
Step-by-Step Solution
Solution:
  1. Step 1: Understand class hierarchy

    SportsCar inherits from Car, which inherits from Vehicle.
  2. Step 2: Evaluate is_a? and kind_of? calls

    obj.is_a?(Car) and obj.kind_of?(Vehicle) both return true because obj is a SportsCar instance.
  3. Final Answer:

    true -> Option B
  4. Quick Check:

    is_a? and kind_of? true for subclasses and superclasses [OK]
Quick Trick: is_a? and kind_of? check full inheritance chain [OK]
Common Mistakes:
  • Assuming false for grandparent classes
  • Confusing is_a? with instance_of?
  • Expecting error on multiple inheritance levels

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes