Bird
0
0

Given this Ruby code, what will be the output?

hard📝 Application Q9 of 15
Ruby - Control Flow
Given this Ruby code, what will be the output?
value = 0
case value
in 1..5 then puts 'Positive'
in -5..0 then puts 'Negative or Zero'
else puts 'Unknown'
end
APositive
BNegative or Zero
CUnknown
DError
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value and ranges

    The value is 0. The ranges are 1..5 and -5..0.
  2. Step 2: Match value to ranges

    0 is not in 1..5 but is included in -5..0, so it matches the second pattern and prints 'Negative or Zero'.
  3. Final Answer:

    Negative or Zero -> Option B
  4. Quick Check:

    0 in -5..0 = Negative or Zero [OK]
Quick Trick: Check inclusive ranges carefully for boundary values [OK]
Common Mistakes:
MISTAKES
  • Assuming 0 is positive
  • Choosing else instead of matching range
  • Confusing exclusive and inclusive ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes