Bird
0
0

Identify the error in this PowerShell statement:

medium📝 Debug Q14 of 15
PowerShell - Operators
Identify the error in this PowerShell statement:
if (-not $a -and $b) { Write-Output 'Valid' }
ANo error, statement is correct
BIncorrect use of -and operator
CVariables $a and $b are undefined
DMissing parentheses around -not condition
Step-by-Step Solution
Solution:
  1. Step 1: Understand operator precedence

    -not has higher precedence; without parentheses, it applies only to $a, but combined with -and can cause confusion.
  2. Step 2: Fix with parentheses

    Use parentheses: if ((-not $a) -and $b) { ... } to clarify logic and avoid errors.
  3. Final Answer:

    Missing parentheses around -not condition -> Option D
  4. Quick Check:

    Use parentheses with -not in complex expressions [OK]
Quick Trick: Use parentheses to group -not conditions [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Assuming -not applies to whole expression
  • Not using parentheses for clarity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes