Bird
0
0

Identify the error in this PowerShell foreach loop:

medium📝 Debug Q14 of 15
PowerShell - Control Flow
Identify the error in this PowerShell foreach loop:
foreach $item in $array { Write-Output $item }
ANo error, code is correct
BMissing parentheses around the loop variable and collection
CUsing $item instead of $i as loop variable
DIncorrect use of Write-Output
Step-by-Step Solution
Solution:
  1. Step 1: Check foreach syntax

    PowerShell requires parentheses around the variable and collection in foreach loops.
  2. Step 2: Identify missing parentheses

    The code lacks parentheses: it should be foreach ($item in $array) { ... }
  3. Final Answer:

    Missing parentheses around the loop variable and collection -> Option B
  4. Quick Check:

    foreach needs parentheses [OK]
Quick Trick: Always use parentheses in foreach syntax [OK]
Common Mistakes:
  • Omitting parentheses
  • Confusing variable names
  • Thinking Write-Output causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes