Bird
0
0

Which of the following is the correct syntax to assign 10 to variable num only if num is nil or false?

easy📝 Syntax Q3 of 15
Ruby - Operators and Expressions
Which of the following is the correct syntax to assign 10 to variable num only if num is nil or false?
Anum =|| 10
Bnum =| 10
Cnum ||= 10
Dnum = 10 ||
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct operator syntax

    The correct operator for conditional assignment is ||=.
  2. Step 2: Match syntax with variable and value

    num ||= 10 assigns 10 only if num is nil or false.
  3. Final Answer:

    num ||= 10 -> Option C
  4. Quick Check:

    Correct ||= syntax = num ||= 10 [OK]
Quick Trick: Use ||= for conditional assignment in Ruby [OK]
Common Mistakes:
MISTAKES
  • Using =|| instead of ||= operator
  • Placing || after the value
  • Confusing with bitwise or operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes