Bird
0
0

Which of the following is the correct way to override a method named greet in a Ruby child class?

easy📝 Syntax Q12 of 15
Ruby - Inheritance
Which of the following is the correct way to override a method named greet in a Ruby child class?
Aclass Child < Parent\n def greet_child\n puts 'Hello from child'\n end\nend
Bclass Child < Parent\n def greet\n puts 'Hello from child'\n end\nend
Cclass Child < Parent\n def greet() puts 'Hello from child' end\nend
Dclass Child < Parent\n def greet: puts 'Hello from child' end\nend
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes