Bird
0
0

Identify the error in this Ruby class definition:

medium📝 Debug Q14 of 15
Ruby - Classes and Objects
Identify the error in this Ruby class definition:
class Car
  def initialize(make, model)
    make = make
    model = model
  end
end

car = Car.new("Toyota", "Corolla")
puts car.make
AThe initialize method should not have parameters.
BThe puts statement should be puts car.model
CThe class name should be lowercase.
DInstance variables are not assigned; should use @make and @model.
Step-by-Step Solution
Solution:
  1. Step 1: Check variable assignment in initialize

    The code assigns local variables make = make and model = model, which do nothing to the object.
  2. Step 2: Correct usage of instance variables

    To store values in the object, it should assign @make = make and @model = model.
  3. Final Answer:

    Instance variables are not assigned; should use @make and @model. -> Option D
  4. Quick Check:

    Use @variable to store data in object [OK]
Quick Trick: Use @ before variable to save it in the object [OK]
Common Mistakes:
  • Assigning to local variables instead of instance variables
  • Thinking parameters auto become instance variables
  • Wrong class naming conventions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes