Bird
0
0

Consider this Ruby code:

medium📝 Predict Output Q5 of 15
Ruby - Methods
Consider this Ruby code:
class Account
  def active?
    false
  end

  def activate!
    @active = true
  end
end

acc = Account.new
puts acc.active?
acc.activate!
puts acc.active?

What is the output?
Atrue\ntrue
Bfalse\ntrue
Cfalse\nfalse
Dtrue\nfalse
Step-by-Step Solution
Solution:
  1. Step 1: Check active? method

    It always returns false, ignoring any instance variables.
  2. Step 2: Check activate! method

    It sets @active = true but active? does not use @active, so output remains unchanged.
  3. Final Answer:

    false\nfalse -> Option C
  4. Quick Check:

    Method must use instance vars to reflect changes [OK]
Quick Trick: ? methods must reflect state to change output [OK]
Common Mistakes:
  • Assuming activate! changes active? output
  • Ignoring method implementation details
  • Confusing variable assignment with method return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes