Bird
0
0

Given the module and classes below, what will puts Cat.new.sound output?

hard📝 Application Q15 of 15
Ruby - Modules and Mixins
Given the module and classes below, what will puts Cat.new.sound output?
module AnimalSounds
  def sound
    "Generic sound"
  end
end

class Dog
  include AnimalSounds
  def sound
    "Bark"
  end
end

class Cat
  include AnimalSounds
end
ABark
BNoMethodError
CGeneric sound
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions in classes

    Class Dog overrides sound method with "Bark". Class Cat does not override it.
  2. Step 2: Determine method lookup for Cat instance

    Since Cat includes AnimalSounds and does not override sound, calling sound on Cat.new uses the module's method returning "Generic sound".
  3. Final Answer:

    Generic sound -> Option C
  4. Quick Check:

    Included method used if no override [OK]
Quick Trick: Class method overrides module method if defined [OK]
Common Mistakes:
  • Assuming Cat uses Dog's sound method
  • Expecting NoMethodError for missing method
  • Confusing instance and class methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes