Bird
0
0

Which of these is a valid use of the ternary operator in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Operators and Expressions
Which of these is a valid use of the ternary operator in Ruby?
Aresult = score > 50 ? 'Pass' : 'Fail'
Bif score > 50 then 'Pass' else 'Fail' end
Cscore > 50 ? 'Pass' and 'Fail'
Dresult = score > 50 : 'Pass' ? 'Fail'
Step-by-Step Solution
Solution:
  1. Step 1: Check the syntax of each option

    result = score > 50 ? 'Pass' : 'Fail' uses the correct ternary format: condition ? true_value : false_value. if score > 50 then 'Pass' else 'Fail' end is an if-else statement, not ternary. score > 50 ? 'Pass' and 'Fail' uses 'and' incorrectly. result = score > 50 : 'Pass' ? 'Fail' mixes symbols wrongly.
  2. Step 2: Confirm the correct ternary syntax

    Only result = score > 50 ? 'Pass' : 'Fail' correctly assigns 'Pass' or 'Fail' based on the condition using the ternary operator.
  3. Final Answer:

    result = score > 50 ? 'Pass' : 'Fail' -> Option A
  4. Quick Check:

    Valid ternary syntax = result = score > 50 ? 'Pass' : 'Fail' [OK]
Quick Trick: Ternary uses ? and : in order [OK]
Common Mistakes:
  • Using 'and' instead of ':'
  • Swapping '?' and ':'
  • Writing if-else instead of ternary

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes