Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Variables and Data Types
What will this Ruby code print?
value = nil
if !value
  puts "No value"
else
  puts "Has value"
end
Anil
BNo value
CHas value
DError
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate !value when value is nil

    Since nil is falsey, !nil is true.
  2. Step 2: Determine which branch runs

    The if condition is true, so it prints "No value".
  3. Final Answer:

    No value -> Option B
  4. Quick Check:

    !nil is true, prints No value [OK]
Quick Trick: Negation of nil is true in Ruby [OK]
Common Mistakes:
  • Thinking !nil is false
  • Confusing nil with false
  • Expecting error from nil negation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes