Bird
0
0

Consider this Ruby code:

hard📝 Application Q9 of 15
Ruby - Operators and Expressions
Consider this Ruby code:
def check(value)
  !value || value == 0
end

puts check(nil)
puts check(0)
puts check(5)

What will be the output?
Afalse false true
Btrue true false
Ctrue false true
Dfalse true false
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate check(nil)

    !nil is true, so true || false = true.
  2. Step 2: Evaluate check(0)

    !0 is false (0 is truthy in Ruby), but 0 == 0 is true, so false || true = true.
  3. Step 3: Evaluate check(5)

    !5 is false, 5 == 0 is false, so false || false = false.
  4. Final Answer:

    Outputs are true true false -> Option B
  5. Quick Check:

    !value || value == 0 returns true for nil and 0 only [OK]
Quick Trick: !value is true only for nil or false in Ruby [OK]
Common Mistakes:
  • Assuming 0 is false
  • Misunderstanding ! operator
  • Ignoring Ruby truthiness rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes