Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q4 of 15
Ruby - Classes and Objects
What will this Ruby code output?
class Car
  def initialize(make)
    @make = make
  end
  def show_make
    puts @make
  end
end
car = Car.new("Toyota")
car.show_make
AMake
Bcar
CToyota
DError: undefined method
Step-by-Step Solution
Solution:
  1. Step 1: Understand object creation and initialize

    The Car.new("Toyota") calls initialize with "Toyota", setting @make to "Toyota".
  2. Step 2: Check method output

    The show_make method prints the value of @make, which is "Toyota".
  3. Final Answer:

    Toyota -> Option C
  4. Quick Check:

    initialize sets @make = Toyota, show_make prints it [OK]
Quick Trick: initialize sets instance vars used by other methods [OK]
Common Mistakes:
  • Expecting method name printed
  • Confusing object name with attribute
  • Thinking code raises error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes