Bird
0
0

What will be printed by this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Variables and Data Types
What will be printed by this Ruby code?
val = nil
if val.nil?
  puts "Empty"
else
  puts "Contains value"
end
AContains value
BEmpty
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition

    The condition val.nil? returns true because val is nil.
  2. Step 2: Determine output

    Since the condition is true, puts "Empty" executes.
  3. Final Answer:

    Empty -> Option B
  4. Quick Check:

    Running the code prints "Empty" [OK]
Quick Trick: nil.nil? returns true, so first branch runs [OK]
Common Mistakes:
  • Assuming nil is false in condition without .nil?
  • Expecting 'Contains value' output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes