Bird
0
0

What will be printed when this Ruby code runs?

medium📝 Predict Output Q4 of 15
Ruby - Inheritance
What will be printed when this Ruby code runs?
class Vehicle
  def start
    'Starting vehicle'
  end
end

class Car < Vehicle
end

puts Car.new.start
AStarting vehicle
BError: undefined method 'start'
CStarting car
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze class inheritance

    Class Car inherits from Vehicle.
  2. Step 2: Check method availability

    Vehicle defines start, so Car instances have access to it.
  3. Step 3: Output determination

    Calling start on Car.new prints 'Starting vehicle'.
  4. Final Answer:

    Starting vehicle -> Option A
  5. Quick Check:

    Subclass inherits parent methods [OK]
Quick Trick: Subclass inherits methods from parent class [OK]
Common Mistakes:
  • Expecting method to be undefined
  • Assuming output is 'Starting car'
  • Thinking no output will occur

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes