Bird
0
0

What is the output of this Ruby code combining OOP and functional style?

medium📝 Predict Output Q4 of 15
Ruby - Functional Patterns in Ruby
What is the output of this Ruby code combining OOP and functional style?
class Counter
  def initialize
    @count = 0
  end

  def increment
    @count += 1
  end

  def current
    @count
  end
end

counter = Counter.new
3.times { counter.increment }
puts counter.current
A1
B0
CError
D3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the Counter class behavior

    The class starts with @count = 0 and increments it by 1 each time increment is called.
  2. Step 2: Trace the code execution

    increment is called 3 times, so @count becomes 3; current returns @count.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Counter increments 3 times = 3 [OK]
Quick Trick: Increment method updates count; current returns it [OK]
Common Mistakes:
  • Assuming @count resets each time increment is called
  • Confusing method names or forgetting to call increment
  • Expecting output before increment calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes