Bird
0
0

Find the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Control Flow

Find the error in this Ruby code snippet:

score = 85
case score
when 90..100
  puts "A"
when 80..89
  puts "B"
when 70..79
  puts "C"
else
  puts "F"
end

Why does it not print "B" for score 85?

ARanges in when must be strings, not numbers.
BThe case statement compares score with ranges incorrectly; use <code>case</code> with <code>when</code> and <code>===</code>.
CThe code is correct; it will print "B" as expected.
DThe ranges overlap and cause confusion.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how case/when works with ranges

    Ruby's case/when uses the === operator, which works with ranges to check if a value is inside.
  2. Step 2: Check the given score and ranges

    Score 85 falls inside 80..89, so puts "B" will run correctly.
  3. Final Answer:

    The code is correct; it will print "B" as expected. -> Option C
  4. Quick Check:

    Ranges work with case/when using === [OK]
Quick Trick: Ranges work naturally in when clauses with case [OK]
Common Mistakes:
MISTAKES
  • Thinking ranges must be strings
  • Believing case/when can't handle ranges
  • Assuming overlapping ranges cause errors here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes