Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Advanced Metaprogramming
What will be the output of this Ruby code?
class Parent
  def self.inherited(subclass)
    puts "Subclass: #{subclass}"
  end
end

class Child < Parent
end
ANo output
BSubclass: Child
CSubclass: Parent
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand when inherited runs

    The inherited method in Parent runs automatically when a subclass is created.
  2. Step 2: Identify subclass creation

    When Child < Parent is declared, Parent.inherited(Child) is called, printing "Subclass: Child".
  3. Final Answer:

    Subclass: Child -> Option B
  4. Quick Check:

    Subclass creation triggers inherited hook output [OK]
Quick Trick: Inherited prints subclass name on subclass creation [OK]
Common Mistakes:
  • Expecting output on instance creation
  • Confusing subclass with parent class name
  • Thinking no output occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes