Bird
0
0

Find the error in this PowerShell code using ternary operator:

medium📝 Debug Q7 of 15
PowerShell - Operators
Find the error in this PowerShell code using ternary operator:
$score = 85
$grade = $score -ge 90 ? 'A' : $score -ge 80 ? 'B' : 'C'
ATernary operator cannot be nested like this
BMissing parentheses to group nested ternary expressions
CIncorrect comparison operator used
DVariable $grade is not declared
Step-by-Step Solution
Solution:
  1. Step 1: Understand nested ternary syntax

    Nested ternary operators require parentheses to clarify grouping.
  2. Step 2: Identify missing parentheses

    The code lacks parentheses around the nested ternary, causing ambiguity.
  3. Final Answer:

    Missing parentheses to group nested ternary expressions -> Option B
  4. Quick Check:

    Use parentheses to nest ternary operators correctly [OK]
Quick Trick: Wrap nested ternaries in parentheses to avoid errors [OK]
Common Mistakes:
  • Ignoring parentheses in nested ternaries
  • Assuming nesting not allowed
  • Confusing comparison operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes