Bird
0
0

Given this code, what will be the output?

hard📝 Application Q8 of 15
Ruby - Inheritance
Given this code, what will be the output?
class A
  def call_me(x)
    x * 2
  end
end

class B < A
  def call_me(x)
    super + 5
  end
end

class C < B
  def call_me(x)
    super(x + 1)
  end
end

puts C.new.call_me(4)
A15
B11
C10
DError due to wrong arguments
Step-by-Step Solution
Solution:
  1. Step 1: Trace call from C

    C's call_me(4) calls super(4 + 1) = super(5) which calls B's method with 5.
  2. Step 2: Evaluate B's method

    B's call_me(5) calls super (A's method) with 5, which returns 5 * 2 = 10, then adds 5, total 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    super calls chain with argument changes and additions [OK]
Quick Trick: Track arguments and additions through super chain carefully [OK]
Common Mistakes:
  • Ignoring argument change in C's super call
  • Forgetting to add in B's method
  • Assuming output matches first method only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes