Bird
0
0

What will be the output of this Ruby code?

hard📝 Application Q8 of 15
Ruby - Inheritance
What will be the output of this Ruby code?
class Person
  def full_name
    "Alice Smith"
  end
end

class Employee < Person
  def full_name
    "Employee: " + super
  end
end

puts Employee.new.full_name
A"Employee: Alice Smith"
B"Alice Smith"
C"Employee: "
DError: undefined method 'super'
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding

    Employee#full_name overrides Person#full_name but calls super to get the parent's string.
  2. Step 2: String concatenation

    The returned string is "Employee: " concatenated with "Alice Smith".
  3. Final Answer:

    "Employee: Alice Smith" -> Option A
  4. Quick Check:

    super returns parent's string; concatenation produces final output [OK]
Quick Trick: super returns parent method's string; concatenate as needed [OK]
Common Mistakes:
  • Expecting only parent's string without prefix
  • Thinking super is undefined
  • Ignoring concatenation in child method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes