Bird
0
0

Which of the following correctly demonstrates the ternary operator usage in PowerShell 7+ to assign $result based on $value being greater than 10?

easy📝 Syntax Q3 of 15
PowerShell - Operators
Which of the following correctly demonstrates the ternary operator usage in PowerShell 7+ to assign $result based on $value being greater than 10?
A$result = $value > 10 ? 'Greater' : 'Lesser or Equal'
B$result = if ($value -gt 10) { 'Greater' } else { 'Lesser or Equal' }
C$result = $value -gt 10 ? 'Greater' : 'Lesser or Equal'
D$result = $value -gt 10 ? 'Greater' ? 'Lesser or Equal'
Step-by-Step Solution
Solution:
  1. Step 1: Understand the ternary syntax

    The ternary operator in PowerShell 7+ uses the syntax: condition ? true-value : false-value.
  2. Step 2: Analyze each option

    $result = $value -gt 10 ? 'Greater' : 'Lesser or Equal' correctly uses -gt for comparison and the ternary operator with colon separating true and false values.
    $result = if ($value -gt 10) { 'Greater' } else { 'Lesser or Equal' } uses an if-else statement, not the ternary operator.
    $result = $value > 10 ? 'Greater' : 'Lesser or Equal' uses > which is valid in PowerShell but the ternary operator requires -gt for clarity and consistency.
    $result = $value -gt 10 ? 'Greater' ? 'Lesser or Equal' has two question marks, which is invalid syntax.
  3. Final Answer:

    $result = $value -gt 10 ? 'Greater' : 'Lesser or Equal' -> Option C
  4. Quick Check:

    Correct ternary syntax uses one question mark and a colon [OK]
Quick Trick: Ternary uses '?' and ':' with a condition [OK]
Common Mistakes:
  • Using multiple question marks instead of one
  • Omitting the colon ':' separator
  • Confusing if-else with ternary syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes