Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Control Flow

What will this Ruby code output?

value = 'b'
case value.downcase
when 'a'
  puts 'Letter A'
when 'b'
  puts 'Letter B'
else
  puts 'Unknown'
end
ALetter A
BError
CLetter B
DUnknown
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate the case expression

    value is 'b', calling downcase keeps it 'b'.
  2. Step 2: Match the when clauses

    when 'b' matches, so it prints 'Letter B'.
  3. Final Answer:

    Letter B -> Option C
  4. Quick Check:

    downcase matches when 'b' = Letter B [OK]
Quick Trick: Use methods in case expression for flexible matching [OK]
Common Mistakes:
MISTAKES
  • Ignoring downcase effect
  • Choosing else incorrectly
  • Expecting error due to method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes