Bird
0
0

Find the error in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Class Methods and Variables
Find the error in this Ruby code:
class Animal
  def self.sound
    puts "Roar"
  end

  def sound
    puts "Meow"
  end
end

animal = Animal.new
animal.sound
Animal.sound()
AError: Missing parentheses in method call
BError: Class method called on instance
CError: Cannot define instance and class methods with same name
DNo error; code runs and prints Meow then Roar
Step-by-Step Solution
Solution:
  1. Step 1: Review method definitions and calls

    Instance method sound prints "Meow"; class method self.sound prints "Roar".
  2. Step 2: Trace execution

    animal.sound calls instance method printing "Meow"; Animal.sound() calls class method printing "Roar".
  3. Final Answer:

    No error; code runs and prints Meow then Roar -> Option D
  4. Quick Check:

    Instance and class methods coexist and run correctly [OK]
Quick Trick: Instance and class methods with same name work independently [OK]
Common Mistakes:
  • Thinking parentheses are mandatory in method calls
  • Confusing instance and class method calls
  • Assuming naming conflict causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes