Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Ruby - Operators and Expressions
Consider this code:
options = { retries: false }
options[:retries] ||= 3
puts options[:retries]

What will be printed and why?
A3, because false triggers ||= assignment
Bfalse, because key exists
Cnil, because false is ignored
DError, because false is invalid for ||= operator
Step-by-Step Solution
Solution:
  1. Step 1: Check initial value of options[:retries]

    It is false, which is falsy.
  2. Step 2: Apply ||= operator

    Since false, options[:retries] ||= 3 assigns 3.
  3. Final Answer:

    3, because false triggers ||= assignment -> Option A
  4. Quick Check:

    False triggers ||= assignment on hash keys [OK]
Quick Trick: False triggers ||= assignment even in hashes [OK]
Common Mistakes:
MISTAKES
  • Assuming existing key prevents assignment
  • Thinking false is truthy
  • Expecting error with false value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes