Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Control Flow
What will this Ruby code print?
value = nil
unless value
  puts 'No value'
else
  puts 'Has value'
end
AError
BNo value
Cnil
DHas value
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition

    In Ruby, nil is falsey, so value is false in condition.
  2. Step 2: Apply unless logic

    unless value runs code if value is false or nil, so it prints 'No value'.
  3. Final Answer:

    No value -> Option B
  4. Quick Check:

    unless falsey runs code [OK]
Quick Trick: unless runs when condition is false or nil [OK]
Common Mistakes:
  • Assuming nil is true
  • Confusing unless with if
  • Expecting error on nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes