Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Operators and Expressions
What will this Ruby code output?
result = 15 <=> 7
puts result
A-1
B0
C1
Dnil
Step-by-Step Solution
Solution:
  1. Step 1: Compare 15 and 7 using spaceship operator

    Since 15 is greater than 7, the operator returns 1.
  2. Step 2: Print the result

    The variable result holds 1, so puts result prints 1.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    Greater comparison = 1 [OK]
Quick Trick: Greater value returns 1 with <=> [OK]
Common Mistakes:
MISTAKES
  • Confusing with -1 or 0
  • Expecting boolean true/false
  • Forgetting to assign result

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes