Only foreach ($item in $list) { Write-Output $item } uses the correct 'in' keyword and parentheses.
Final Answer:
foreach ($item in $list) { Write-Output $item } -> Option A
Quick Check:
Syntax uses 'in' keyword inside parentheses [OK]
Hint: 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
3. What will be the output of this PowerShell script?
foreach ($num in 1..3) { Write-Output ($num * 2) }
medium
A. 2 4 6
B. 1 2 3
C. 3 6 9
D. Error: Invalid syntax
Solution
Step 1: Understand the loop range
The loop runs for numbers 1, 2, and 3.
Step 2: Calculate output for each iteration
Each number is multiplied by 2, so outputs are 2, 4, and 6.
Final Answer:
2 4 6 -> Option A
Quick Check:
1*2=2, 2*2=4, 3*2=6 [OK]
Hint: Multiply each loop number by 2 mentally [OK]
Common Mistakes:
Confusing the range operator '..'
Thinking output is original numbers
Expecting syntax error
4. Identify the error in this PowerShell foreach loop:
foreach $item in $array { Write-Output $item }
medium
A. No error, code is correct
B. Missing parentheses around the loop variable and collection
C. Using $item instead of $i as loop variable
D. Incorrect use of Write-Output
Solution
Step 1: Check foreach syntax
PowerShell requires parentheses around the variable and collection in foreach loops.
Step 2: Identify missing parentheses
The code lacks parentheses: it should be foreach ($item in $array) { ... }
Final Answer:
Missing parentheses around the loop variable and collection -> Option B
Quick Check:
foreach needs parentheses [OK]
Hint: Always use parentheses in foreach syntax [OK]
Common Mistakes:
Omitting parentheses
Confusing variable names
Thinking Write-Output causes error
5. You have a list of filenames stored in $files. You want to print only those filenames that end with '.txt' using a foreach loop. Which script correctly does this?
hard
A. foreach ($file in $files) { if ($file -eq '*.txt') { Write-Output $file } }
B. foreach ($file in $files) { if ($file contains '.txt') { Write-Output $file } }
C. foreach ($file in $files) { Write-Output $file -match '.txt' }
D. foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } }
Solution
Step 1: Understand filtering condition
To check if a filename ends with '.txt', use the '-like' operator with '*.txt' pattern.
Step 2: Verify foreach loop and condition
foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } } correctly uses foreach with an if condition checking '-like "*.txt"' and outputs matching files.
Final Answer:
foreach ($file in $files) { if ($file -like '*.txt') { Write-Output $file } } -> Option D
Quick Check:
Use '-like' with wildcard for pattern match [OK]
Hint: Use '-like "*.txt"' inside if in foreach [OK]
Common Mistakes:
Using '-eq' instead of '-like' for pattern matching
Misusing '-match' without if condition
Using 'contains' which is invalid here
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.