Bird
0
0

Which of the following is the correct syntax for a foreach loop in PowerShell?

easy📝 Syntax Q12 of 15
PowerShell - Control Flow
Which of the following is the correct syntax for a foreach loop in PowerShell?
Aforeach ($item in $list) { Write-Output $item }
Bforeach $item in $list { Write-Output $item }
Cforeach ($item : $list) { Write-Output $item }
Dforeach ($item; $list) { Write-Output $item }
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct foreach syntax

    PowerShell foreach loops use: foreach ($var in $collection) { ... }
  2. Step 2: Match options to syntax

    Only foreach ($item in $list) { Write-Output $item } uses the correct 'in' keyword and parentheses.
  3. Final Answer:

    foreach ($item in $list) { Write-Output $item } -> Option A
  4. Quick Check:

    Syntax uses 'in' keyword inside parentheses [OK]
Quick Trick: Look for 'foreach ($var in $collection)' pattern [OK]
Common Mistakes:
  • Omitting parentheses around variable and collection
  • Using ':' or ';' instead of 'in'
  • Missing the 'in' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes