Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Ruby - Modules and Mixins
What will this code print?
module M
  def greet
    "M says hello"
  end
end

class C
  prepend M
  def greet
    "C says hello"
  end
end

puts C.new.greet
AM says hello
BC says hello
CM says helloC says hello
DError: super not defined
Step-by-Step Solution
Solution:
  1. Step 1: Identify method lookup order

    Since M is prepended, its greet method is called before C's.
  2. Step 2: Check method calls

    M's greet returns "M says hello" and does not call super, so C's method is not called.
  3. Final Answer:

    M says hello -> Option A
  4. Quick Check:

    Prepended module method runs first and returns its string [OK]
Quick Trick: Prepended module method runs first, super needed to call class method [OK]
Common Mistakes:
  • Assuming class method runs first
  • Expecting concatenated output
  • Thinking super is called automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes