Bird
Raised Fist0
Rubyprogramming~20 mins

Method overriding in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Method Overriding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overridden method in subclass
What is the output of the following Ruby code?
Ruby
class Animal
  def speak
    "Hello from Animal"
  end
end

class Dog < Animal
  def speak
    "Woof!"
  end
end

puts Dog.new.speak
AHello from Animal
BRuntimeError
Cnil
DWoof!
Attempts:
2 left
💡 Hint
The subclass method with the same name replaces the parent method.
❓ Predict Output
intermediate
2:00remaining
Calling super in overridden method
What will this Ruby code print?
Ruby
class Parent
  def greet
    "Hi"
  end
end

class Child < Parent
  def greet
    super + ", there!"
  end
end

puts Child.new.greet
Athere!
BHi, there!
CHi
DNoMethodError
Attempts:
2 left
💡 Hint
super calls the parent method's version.
❓ Predict Output
advanced
2:00remaining
Overriding method with different parameters
What is the output of this Ruby code?
Ruby
class Base
  def info(x)
    "Base: #{x}"
  end
end

class Derived < Base
  def info(x, y = nil)
    if y
      "Derived: #{x} and #{y}"
    else
      super(x)
    end
  end
end

puts Derived.new.info(5)
puts Derived.new.info(5, 10)
A
Base: 5
Derived: 5 and 10
B
Derived: 5
Derived: 5 and 10
CArgumentError
D
Base: 5
Base: 5
Attempts:
2 left
💡 Hint
The overridden method uses default parameter and calls super conditionally.
❓ Predict Output
advanced
2:00remaining
Effect of overriding method without calling super
What will this Ruby code output?
Ruby
class Vehicle
  def description
    "Vehicle base description"
  end
end

class Car < Vehicle
  def description
    "Car specific description"
  end
end

puts Vehicle.new.description
puts Car.new.description
A
Vehicle base description
Car specific description
B
Vehicle base description
Vehicle base description
C
Car specific description
Car specific description
DNoMethodError
Attempts:
2 left
💡 Hint
Overriding replaces the parent method unless super is called.
🧠 Conceptual
expert
3:00remaining
Understanding method lookup with overriding and modules
Given this Ruby code, what is the output?
Ruby
module M
  def greet
    "Hello from M"
  end
end

class A
  def greet
    "Hello from A"
  end
end

class B < A
  include M
  def greet
    super + " and B"
  end
end

puts B.new.greet
ANoMethodError
BHello from A and B
CHello from M and B
DHello from B
Attempts:
2 left
💡 Hint
Modules included in a class come before the superclass in method lookup.

Practice

(1/5)
1. What does method overriding in Ruby allow a child class to do?
easy
A. Prevent the parent class from being used
B. Create a new method with a different name
C. Change the behavior of a method inherited from the parent class
D. Automatically call all parent methods

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class uses the same method name as the parent but changes its behavior.
  2. Step 2: Identify the effect on behavior

    The child class method replaces the parent's method behavior when called on the child object.
  3. Final Answer:

    Change the behavior of a method inherited from the parent class -> Option C
  4. Quick Check:

    Method overriding = change inherited method behavior [OK]
Hint: Same method name in child changes parent method behavior [OK]
Common Mistakes:
  • Thinking overriding creates a new method with a different name
  • Believing it prevents parent class usage
  • Assuming it calls all parent methods automatically
2. Which of the following is the correct way to override a method named greet in a Ruby child class?
easy
A. class Child < Parent\n def greet_child\n puts 'Hello from child'\n end\nend
B. class Child < Parent\n def greet\n puts 'Hello from child'\n end\nend
C. class Child < Parent\n def greet() puts 'Hello from child' end\nend
D. class Child < Parent\n def greet: puts 'Hello from child' end\nend

Solution

  1. Step 1: Check method name matches exactly

    Overriding requires the child method to have the same name as the parent method, here 'greet'.
  2. Step 2: Verify Ruby method syntax

    Ruby method definitions use def method_name followed by body and end. Multi-line with indentation is standard. Parentheses are optional, but def greet() puts ... end is valid syntax if properly formatted. Colon syntax is invalid.
  3. Final Answer:

    class Child < Parent\n def greet\n puts 'Hello from child'\n end\nend -> Option B
  4. Quick Check:

    Same method name + correct syntax = override [OK]
Hint: Use same method name and proper def/end syntax [OK]
Common Mistakes:
  • Changing method name instead of overriding
  • Using incorrect method definition syntax
  • Adding colon after method name
3. What will be the output of this Ruby code?
class Parent
  def greet
    puts 'Hello from Parent'
  end
end

class Child < Parent
  def greet
    puts 'Hello from Child'
  end
end

obj = Child.new
obj.greet
medium
A. Hello from Child
B. Error: undefined method greet
C. No output
D. Hello from Parent

Solution

  1. Step 1: Identify class hierarchy and method overriding

    Child class overrides the greet method of Parent with its own version.
  2. Step 2: Determine which method is called

    Creating an instance of Child and calling greet calls the Child's version, printing 'Hello from Child'.
  3. Final Answer:

    Hello from Child -> Option A
  4. Quick Check:

    Child method overrides parent = Child output [OK]
Hint: Child instance calls overridden method, not parent's [OK]
Common Mistakes:
  • Expecting parent method output
  • Thinking no output occurs
  • Assuming method is undefined
4. Find the error in this Ruby code that tries to override a method:
class Parent
  def speak
    puts 'Parent speaks'
  end
end

class Child < Parent
  def speak()
    puts 'Child speaks'
  end
medium
A. Cannot override methods with parentheses
B. Method name should be different
C. Parent class must be declared inside Child
D. Missing end for Child class

Solution

  1. Step 1: Check class and method block endings

    The Child class is missing the closing end keyword to finish the class definition.
  2. Step 2: Confirm method overriding syntax

    The method speak is correctly named and defined; parentheses are allowed but optional.
  3. Final Answer:

    Missing end for Child class -> Option D
  4. Quick Check:

    Every class needs matching end [OK]
Hint: Count your end keywords carefully [OK]
Common Mistakes:
  • Thinking method name must change
  • Believing parentheses cause error
  • Confusing class nesting rules
5. Given this Ruby code, what will be the output?
class Animal
  def sound
    'Some sound'
  end
end

class Dog < Animal
  def sound
    super + ' and Bark'
  end
end

class Cat < Animal
  def sound
    super + ' and Meow'
  end
end

puts Dog.new.sound
puts Cat.new.sound
hard
A. Some sound and Bark\nSome sound and Meow
B. Some sound\nSome sound
C. and Bark\nand Meow
D. Error: super called incorrectly

Solution

  1. Step 1: Understand use of super in overridden methods

    Both Dog and Cat override sound and call super to get Animal's sound return value.
  2. Step 2: Combine parent and child method results

    Each child adds its own string to the parent's 'Some sound', resulting in 'Some sound and Bark' and 'Some sound and Meow'.
  3. Final Answer:

    Some sound and Bark\nSome sound and Meow -> Option A
  4. Quick Check:

    super adds parent method result [OK]
Hint: super calls parent method result to extend behavior [OK]
Common Mistakes:
  • Ignoring super and expecting only child string
  • Expecting error from super usage
  • Thinking output is only parent's string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard