Bird
0
0

You want to categorize a number into 'small', 'medium', or 'large' using a case/when statement. Which code correctly implements this?

hard📝 Application Q8 of 15
Ruby - Control Flow

You want to categorize a number into 'small', 'medium', or 'large' using a case/when statement. Which code correctly implements this?

Acase num when 0,10 'small' when 11,100 'medium' else 'large' end
Bcase num when 0..10 then 'small' when 11..100 then 'medium' when 101.. then 'large' end
Ccase num when 0..10 'small' when 11..100 'medium' when 101..1000 'large' end
Dcase num when 0..10 'small' when 11..100 'medium' else 'large' end
Step-by-Step Solution
Solution:
  1. Step 1: Check coverage of all number ranges

    case num when 0..10 'small' when 11..100 'medium' else 'large' end covers 0..10 as small, 11..100 as medium, and else as large (all others).
  2. Step 2: Verify syntax and completeness

    case num when 0..10 'small' when 11..100 'medium' else 'large' end uses valid syntax and covers all cases with else.
  3. Final Answer:

    case num when 0..10 'small' when 11..100 'medium' else 'large' end -> Option D
  4. Quick Check:

    Use else for catch-all in case/when [OK]
Quick Trick: Use else for all other cases in case/when [OK]
Common Mistakes:
  • Missing else for large numbers
  • Using incomplete ranges
  • Incorrect range syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes