Bird
0
0

Which of these is a valid use of the ternary operator in PowerShell 7+?

easy📝 Conceptual Q2 of 15
PowerShell - Operators
Which of these is a valid use of the ternary operator in PowerShell 7+?
A$x = 5; if ($x -gt 3) { 'Yes' } else { 'No' }
B$x = 5; $result = $x -gt 3 ? 'Yes' : 'No'
C$x = 5; $result = if $x -gt 3 then 'Yes' else 'No'
D$x = 5; $result = $x gt 3 ? 'Yes' : 'No'
Step-by-Step Solution
Solution:
  1. Step 1: Check PowerShell ternary syntax

    PowerShell uses the syntax: condition ? true-value : false-value. $x = 5; $result = $x -gt 3 ? 'Yes' : 'No' matches this exactly.
  2. Step 2: Identify incorrect syntax in other options

    $x = 5; if ($x -gt 3) { 'Yes' } else { 'No' } uses if-else block, not ternary. $x = 5; $result = if $x -gt 3 then 'Yes' else 'No' uses invalid keywords 'then' and 'else'. $x = 5; $result = $x gt 3 ? 'Yes' : 'No' uses 'gt' which is invalid in PowerShell (requires '-gt').
  3. Final Answer:

    $x = 5; $result = $x -gt 3 ? 'Yes' : 'No' -> Option B
  4. Quick Check:

    Valid ternary syntax = $x = 5; $result = $x -gt 3 ? 'Yes' : 'No' [OK]
Quick Trick: Use -gt, not >, with ternary in PowerShell [OK]
Common Mistakes:
  • Using gt without dash for comparison
  • Mixing if-else block with ternary syntax
  • Using invalid keywords like then

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes