Bird
0
0

Which of the following is the correct syntax for a case statement in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Control Flow
Which of the following is the correct syntax for a case statement in Ruby?
Acase x { when 1: puts 'One'; else: puts 'Other' }
Bcase x when 1 then puts 'One' else puts 'Other' end
Cswitch (x) { case 1: puts 'One'; default: puts 'Other'; }
Dif x == 1 then puts 'One' else puts 'Other' end
Step-by-Step Solution
Solution:
  1. Step 1: Identify Ruby's case syntax

    Ruby uses case followed by when clauses and ends with end.
  2. Step 2: Compare options

    case x when 1 then puts 'One' else puts 'Other' end matches Ruby syntax; others use different language styles or incorrect syntax.
  3. Final Answer:

    case x when 1 then puts 'One' else puts 'Other' end -> Option B
  4. Quick Check:

    Correct Ruby case syntax = B [OK]
Quick Trick: case uses when and ends with end [OK]
Common Mistakes:
MISTAKES
  • Using curly braces instead of end
  • Confusing Ruby with JavaScript or C syntax
  • Using switch instead of case

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes