Bird
0
0

Find the error in this code:

medium📝 Debug Q7 of 15
PowerShell - File and Directory Operations
Find the error in this code:
$exists = Test-Path 'C:\Temp\data.txt'
if $exists {
  Write-Output 'File found'
}
ANo error, code is correct
BMissing parentheses around if condition
CWrite-Output is invalid here
DTest-Path cannot be assigned to a variable
Step-by-Step Solution
Solution:
  1. Step 1: Review if statement syntax

    In PowerShell, the condition in if must be enclosed in parentheses.
  2. Step 2: Identify missing parentheses

    The code uses if $exists { ... } without parentheses, which is a syntax error.
  3. Final Answer:

    Missing parentheses around if condition -> Option B
  4. Quick Check:

    If condition needs parentheses = Missing parentheses around if condition [OK]
Quick Trick: Always use parentheses around if conditions [OK]
Common Mistakes:
  • Omitting parentheses in if
  • Assuming variable assignment is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes