Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Control Flow
What is the output of this Ruby code?
count = 0
count += 1 if true
count += 1 unless false
puts count
A2
B0
C1
DError
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate first increment

    count += 1 if true runs because condition is true, count becomes 1.
  2. Step 2: Evaluate second increment

    count += 1 unless false runs because condition is false, count becomes 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Inline if/unless conditions true/false increment count [OK]
Quick Trick: Inline if runs if true; unless runs if false [OK]
Common Mistakes:
  • Confusing unless false as not running
  • Forgetting increments
  • Expecting syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes