Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Inheritance
Identify the error in this Ruby code snippet:
class Parent
  def greet
    "Hello"
  end
end

class Child < Parent
  def greet
    super()
  end
end

Child.new.greet(1)
AError: super called with wrong syntax
BNo error, outputs 'Hello'
CError: wrong number of arguments (given 1, expected 0)
DError: undefined method greet for Child
Step-by-Step Solution
Solution:
  1. Step 1: Check method definitions and arguments

    The parent greet method takes no arguments. The child calls super() which calls parent with no arguments.
  2. Step 2: Check method call on Child instance

    The call Child.new.greet(1) passes 1 argument, but greet expects none, causing an argument error.
  3. Final Answer:

    Error: wrong number of arguments (given 1, expected 0) -> Option C
  4. Quick Check:

    Method call arguments must match definition [OK]
Quick Trick: Method call arguments must match method definition [OK]
Common Mistakes:
  • Calling method with extra arguments
  • Misunderstanding super() usage
  • Expecting no error on argument mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes