Bird
0
0

What is the issue with the following Ruby code snippet?

medium📝 Debug Q6 of 15
Ruby - Operators and Expressions
What is the issue with the following Ruby code snippet?
if x == 5
puts "x is five"
elsif x = 10
puts "x is ten"
end
AUsing double equals '==' in the first condition is incorrect
BMissing 'end' keyword for if statement
CUsing assignment operator '=' instead of comparison '==' in elsif condition
Dputs statement syntax is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Identify the operators used

    The first condition uses '==' which is correct for comparison.
  2. Step 2: Check the elsif condition

    The elsif uses '=' which is an assignment, not a comparison.
  3. Final Answer:

    Using assignment operator '=' instead of comparison '==' in elsif condition -> Option C
  4. Quick Check:

    Replace '=' with '==' in elsif condition [OK]
Quick Trick: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
  • Confusing '=' with '==' in conditions
  • Forgetting to close if statements with 'end'
  • Assuming '==' is invalid syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes