Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Methods
What will be the output of this Ruby code?
class User
  def active?
    true
  end

  def deactivate!
    @active = false
    "User deactivated"
  end
end

user = User.new
puts user.active?
puts user.deactivate!
puts user.active?
Atrue User deactivated true
Btrue User deactivated false
Cfalse User deactivated false
Dtrue User deactivated nil
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the active? method

    This method always returns true because it returns the literal true without checking any variable.
  2. Step 2: Analyze the deactivate! method and second active? call

    deactivate! sets @active = false but active? does not use @active, so it still returns true.
  3. Final Answer:

    true User deactivated true -> Option A
  4. Quick Check:

    active? returns true always = true User deactivated true [OK]
Quick Trick: Check if boolean method uses instance variables or fixed return [OK]
Common Mistakes:
  • Assuming deactivate! changes active? output
  • Confusing method side effects
  • Ignoring method return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes