Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Inheritance
Identify the error in this Ruby code:
class Parent
  def details(age)
    "Age: #{age}"
  end
end

class Child < Parent
  def details
    super
  end
end

puts Child.new.details(30)
Asuper cannot be called without parentheses
BParent's details method is missing a default argument
CChild's details method does not accept any arguments but is called with one
DNo error; code runs correctly
Step-by-Step Solution
Solution:
  1. Step 1: Check method signatures

    Parent's details expects one argument age.
  2. Step 2: Child method signature

    Child's details method takes no arguments but calls super without arguments.
  3. Step 3: Invocation

    Calling Child.new.details(30) passes one argument to Child's method, which does not accept any, causing an argument error.
  4. Final Answer:

    Child's details method does not accept any arguments but is called with one -> Option C
  5. Quick Check:

    Method signatures must match for super calls [OK]
Quick Trick: Child method must accept arguments if parent does [OK]
Common Mistakes:
  • Assuming super fixes argument mismatch automatically
  • Thinking super requires parentheses always
  • Ignoring method signature mismatch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes