Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Classes and Objects
What will be the output of this Ruby code?
class Dog
  def bark
    "Woof!"
  end
end

fido = Dog.new
puts fido.bark
Abark
BWoof!
CDog
DError: undefined method
Step-by-Step Solution
Solution:
  1. Step 1: Understand class and method

    The class Dog has a method bark that returns the string "Woof!".
  2. Step 2: Create object and call method

    We create an object fido from Dog using new. Calling fido.bark returns "Woof!" which is printed.
  3. Final Answer:

    Woof! -> Option B
  4. Quick Check:

    Calling method returns its string output [OK]
Quick Trick: Use .new to create object, call method to get output [OK]
Common Mistakes:
  • Forgetting to use .new to create object
  • Expecting method name instead of return value
  • Confusing class name with method output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes