Bird
0
0

Which Ruby expression correctly checks if variable num is between 15 and 25 inclusive?

hard📝 Application Q8 of 15
Ruby - Operators and Expressions
Which Ruby expression correctly checks if variable num is between 15 and 25 inclusive?
Anum > 15 || num < 25
Bnum >= 15 && num <= 25
Cnum > 15 && num < 25
Dnum == 15 && num == 25
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    Check if num is greater than or equal to 15 and less than or equal to 25.
  2. Step 2: Analyze each option

    num >= 15 && num <= 25 uses '>= 15' and '<= 25' combined with '&&' which is correct.
  3. Step 3: Eliminate incorrect options

    num > 15 || num < 25 uses '||' which allows values outside the range. num > 15 && num < 25 excludes boundaries. num == 15 && num == 25 checks if num equals both 15 and 25 simultaneously, which is impossible.
  4. Final Answer:

    num >= 15 && num <= 25 -> Option B
  5. Quick Check:

    Use '&&' with inclusive operators for range check [OK]
Quick Trick: Use '&&' with >= and <= for inclusive range [OK]
Common Mistakes:
  • Using '||' instead of '&&' for range checks
  • Using strict inequalities excluding boundaries
  • Checking equality to two different values simultaneously

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes