Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Classes and Objects
What will be the output of this Ruby code?
class Cat
  def meow
    "Meow!"
  end
end

kitty = Cat.new
puts kitty.meow
Akitty.meow
BMeow!
CError: undefined method
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Understand class and method call

    The class Cat has a method meow returning "Meow!". Creating an instance kitty and calling meow returns that string.
  2. Step 2: Check output of puts

    puts kitty.meow prints the string returned by meow, which is "Meow!".
  3. Final Answer:

    Meow! -> Option B
  4. Quick Check:

    Method call output = Meow! [OK]
Quick Trick: Calling method on instance prints method's return string [OK]
Common Mistakes:
  • Expecting method name printed
  • Confusing instance with class
  • Missing 'new' to create instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes