Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Control Flow

What is the output of this Ruby code?

grade = 'B'
case grade
when 'A'
  puts "Excellent"
when 'B', 'C'
  puts "Good"
when 'D'
  puts "Pass"
else
  puts "Fail"
end
AFail
BExcellent
CPass
DGood
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value of grade

    The variable grade is set to 'B'.
  2. Step 2: Match grade in case/when

    The when 'B', 'C' line matches 'B', so it runs puts "Good".
  3. Final Answer:

    Good -> Option D
  4. Quick Check:

    grade 'B' matches when 'B', 'C' [OK]
Quick Trick: Check all values in when line, including multiple matches [OK]
Common Mistakes:
  • Only checking first value in when list
  • Confusing else output
  • Ignoring multiple values in one when

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes