Bird
0
0

Find the error in this Swift code using the ternary operator:

medium📝 Debug Q6 of 15
Swift - Operators and Expressions
Find the error in this Swift code using the ternary operator:
let score = 85
let grade = score >= 90 ? "A" : score >= 80 ? "B" : "C"
ANo error, code is correct
BIncorrect use of multiple ternary operators without parentheses
CMissing parentheses for nested ternary expressions
DUsing quotes incorrectly around letters
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nested ternary usage

    Swift allows nested ternary operators without parentheses if the order is clear.
  2. Step 2: Check syntax correctness

    The code correctly assigns "B" for scores between 80 and 89, and "C" otherwise, so no syntax error exists.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Nested ternary without parentheses is valid [OK]
Quick Trick: Nested ternary operators can be used without parentheses [OK]
Common Mistakes:
  • Assuming parentheses always needed
  • Misreading nested ternary logic
  • Thinking quotes cause error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes