Bird
0
0

You want to check if a variable x is either nil or false in Ruby. Which expression correctly uses logical operators to do this?

hard📝 Application Q8 of 15
Ruby - Operators and Expressions
You want to check if a variable x is either nil or false in Ruby. Which expression correctly uses logical operators to do this?
Ax == nil || x == false
Bx && (nil || false)
C!x && x == false
Dx || nil && false
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    We want to check if x is nil OR x is false.
  2. Step 2: Evaluate each option

    x == nil || x == false explicitly checks x == nil OR x == false, which is correct. Others misuse operators or logic.
  3. Final Answer:

    x == nil || x == false correctly checks the condition -> Option A
  4. Quick Check:

    Use explicit comparisons with || for OR checks [OK]
Quick Trick: Use explicit comparisons combined with || for OR checks [OK]
Common Mistakes:
  • Using && instead of ||
  • Misplacing parentheses
  • Using !x incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes