Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Operators and Expressions
What will be the output of this Ruby code?
score = nil
score ||= 15
score ||= 25
puts score
A25
B15
Cnil
D0
Step-by-Step Solution
Solution:
  1. Step 1: Initial value

    score starts as nil.
  2. Step 2: First ||= assignment

    score ||= 15 assigns 15 because score is nil.
  3. Step 3: Second ||= assignment

    score ||= 25 does not assign 25 because score is now 15 (truthy).
  4. Final Answer:

    15 -> Option B
  5. Quick Check:

    Once assigned a truthy value, ||= does not reassign [OK]
Quick Trick: ||= assigns only if variable is nil or false [OK]
Common Mistakes:
  • Assuming second ||= will overwrite the first assignment
  • Confusing nil and 0 as falsey values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes