Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q7 of 15
Ruby - Variables and Data Types
What is wrong with this Ruby code?
if nil
  puts "True"
else
  puts "False"
end
ASyntax error due to nil in if condition
BCode will not run because nil is undefined
Cnil is true, so "True" is printed
Dnil is treated as false, so "False" is printed
Step-by-Step Solution
Solution:
  1. Step 1: Understand how nil behaves in conditionals

    In Ruby, nil is falsey, so the else branch runs.
  2. Step 2: Determine output

    The code prints "False" because the if condition is false.
  3. Final Answer:

    nil is treated as false, so "False" is printed -> Option D
  4. Quick Check:

    nil is falsey in if = B [OK]
Quick Trick: nil acts like false in if statements [OK]
Common Mistakes:
  • Thinking nil is true
  • Expecting syntax error
  • Assuming nil is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes