Bird
0
0

The following Ruby code is intended to print "Falsy" only when var is falsy. What is wrong?

medium📝 Debug Q14 of 15
Ruby - Operators and Expressions
The following Ruby code is intended to print "Falsy" only when var is falsy. What is wrong?
var = nil
if var == false
  puts "Falsy"
else
  puts "Truthy"
end
AIt should check 'if var == nil' only
BSyntax error in the if statement
CIt misses the case when var is nil, which is also falsy
DIt should use 'var != false' instead
Step-by-Step Solution
Solution:
  1. Step 1: Understand falsy values in Ruby

    Both false and nil are falsy in Ruby.
  2. Step 2: Analyze the condition

    The code checks only var == false, so it misses nil which is also falsy.
  3. Final Answer:

    It misses the case when var is nil, which is also falsy -> Option C
  4. Quick Check:

    Falsy = false or nil [OK]
Quick Trick: Check both false and nil for falsy, not just false [OK]
Common Mistakes:
  • Checking only for false and ignoring nil
  • Confusing syntax errors with logic errors
  • Using wrong comparison operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes