Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Inheritance
What will be the output of the following Ruby code?
class Animal; end
class Dog < Animal; end

dog = Dog.new
puts dog.is_a?(Animal)
puts dog.kind_of?(Dog)
puts dog.is_a?(String)
Atrue true false
Bfalse true false
Ctrue false true
Dfalse false true
Step-by-Step Solution
Solution:
  1. Step 1: Understand class inheritance

    Dog inherits from Animal, so a Dog object is both a Dog and an Animal instance.
  2. Step 2: Evaluate each method call

    dog.is_a?(Animal) returns true because Dog is subclass of Animal.
    dog.kind_of?(Dog) returns true because dog is instance of Dog.
    dog.is_a?(String) returns false because dog is not a String.
  3. Final Answer:

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

    Inheritance means is_a? true for parent classes [OK]
Quick Trick: Remember inheritance: subclass instances are also parent class instances [OK]
Common Mistakes:
  • Assuming is_a? false for parent classes
  • Mixing up kind_of? and is_a? results
  • Thinking dog is a String

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes