Bird
0
0

Find the problem in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - Inheritance
Find the problem in this Ruby code snippet:
class BankAccount
  protected
  def balance
    1000
  end
end

account = BankAccount.new
puts account.balance
ASyntax error due to missing method definition
BRuntime error due to undefined method
CPrints 1000 without error
DNoMethodError because protected method called from outside
Step-by-Step Solution
Solution:
  1. Step 1: Understand protected method access

    Protected methods cannot be called from outside the class or its subclasses.
  2. Step 2: Analyze method call

    Calling account.balance from outside raises NoMethodError.
  3. Final Answer:

    NoMethodError because protected method called from outside -> Option D
  4. Quick Check:

    Protected method call outside class = NoMethodError [OK]
Quick Trick: Protected methods inaccessible from outside class instances [OK]
Common Mistakes:
  • Expecting protected methods to be public
  • Confusing protected with private method access
  • Assuming code prints value without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes