Bird
0
0

What will the following Ruby code print?

medium📝 Predict Output Q13 of 15
Ruby - Variables and Data Types
What will the following Ruby code print?
value = nil
if value
  puts "Yes"
else
  puts "No"
end
Anil
BYes
CNo
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand how Ruby treats nil in conditions

    In Ruby, nil is treated as false in conditions.
  2. Step 2: Follow the if-else logic

    Since value is nil, the if value condition is false, so the else branch runs, printing "No".
  3. Final Answer:

    No -> Option C
  4. Quick Check:

    nil acts as false in if [OK]
Quick Trick: nil and false both act as false in conditions [OK]
Common Mistakes:
  • Thinking nil is true
  • Expecting nil to print itself
  • Confusing nil with empty string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes