Bird
0
0

Which of the following shows the correct way to write a case/when statement in Ruby that prints "Hello" when the variable greeting is "hi"?

easy📝 Syntax Q3 of 15
Ruby - Control Flow

Which of the following shows the correct way to write a case/when statement in Ruby that prints "Hello" when the variable greeting is "hi"?

Acase greeting when 'hi' puts 'Hello' else puts 'Goodbye' end
Bcase greeting if 'hi' puts 'Hello' else puts 'Goodbye' end
Cswitch greeting when 'hi' puts 'Hello' else puts 'Goodbye' end
Dcase greeting when greeting == 'hi' puts 'Hello' else puts 'Goodbye' end
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax

    The Ruby case statement uses when clauses to match values directly, not conditions.
  2. Step 2: Analyze options

    case greeting when 'hi' puts 'Hello' else puts 'Goodbye' end correctly uses when 'hi' to match the value of greeting. case greeting if 'hi' puts 'Hello' else puts 'Goodbye' end incorrectly uses if inside case. switch greeting when 'hi' puts 'Hello' else puts 'Goodbye' end uses switch, which is not Ruby syntax. case greeting when greeting == 'hi' puts 'Hello' else puts 'Goodbye' end incorrectly uses a condition inside when.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Correct Ruby case/when syntax matches values directly [OK]
Quick Trick: Use 'when' with values, not conditions [OK]
Common Mistakes:
MISTAKES
  • Using 'if' inside a case statement
  • Using 'switch' instead of 'case'
  • Putting conditions inside 'when' instead of values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes