Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Variables and Data Types
What will be the output of this Ruby code?
value = nil
if value.nil?
  puts "No value"
else
  puts "Has value"
end
AError
BHas value
Cnil
DNo value
Step-by-Step Solution
Solution:
  1. Step 1: Check the variable value

    The variable value is assigned nil, meaning no value.
  2. Step 2: Evaluate the if condition

    The condition value.nil? returns true because value is nil, so it prints "No value".
  3. Final Answer:

    No value -> Option D
  4. Quick Check:

    value.nil? true prints "No value" [OK]
Quick Trick: If variable.nil? is true, it means no value [OK]
Common Mistakes:
  • Assuming nil prints as 'nil'
  • Confusing nil with false
  • Expecting an error from nil check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes