0
0
PowerShellscripting~20 mins

For loop in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell for loop?
Consider the following PowerShell script:
for ($i = 1; $i -le 3; $i++) { Write-Output $i }

What will be the output when this script runs?
PowerShell
for ($i = 1; $i -le 3; $i++) { Write-Output $i }
A
3
2
1
B
0
1
2
3
C1 2 3
D
1
2
3
Attempts:
2 left
💡 Hint
Remember the loop starts at 1 and runs while $i is less than or equal to 3.
📝 Syntax
intermediate
2:00remaining
Which option contains a syntax error in the PowerShell for loop?
Identify the option that will cause a syntax error when running the PowerShell for loop.
Afor ($i=0; $i -lt 5; $i++) { Write-Output $i }
Bfor $i = 0; $i -lt 5; $i++ { Write-Output $i }
Cfor ($i = 0; $i -lt 5; $i++) { Write-Output $i }
Dfor ($i=0; $i -le 4; $i++) { Write-Output $i }
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header carefully.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell for loop not output anything?
Look at this script:
for ($i = 5; $i -lt 3; $i++) { Write-Output $i }

Why does it produce no output?
PowerShell
for ($i = 5; $i -lt 3; $i++) { Write-Output $i }
ABecause the loop condition is false at the start, so the loop body never runs.
BBecause the loop variable $i is not initialized.
CBecause Write-Output is not the correct command to print in PowerShell.
DBecause $i is incremented incorrectly causing an infinite loop.
Attempts:
2 left
💡 Hint
Check the initial value and the loop condition carefully.
🚀 Application
advanced
2:00remaining
How many times will this PowerShell for loop run?
Given this script:
for ($i = 10; $i -ge 5; $i--) { Write-Output $i }

How many times does the loop execute?
PowerShell
for ($i = 10; $i -ge 5; $i--) { Write-Output $i }
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Count from 10 down to 5 inclusive.
🧠 Conceptual
expert
2:00remaining
What is the value of $sum after this PowerShell for loop?
Consider this script:
$sum = 0
for ($i = 1; $i -le 4; $i++) { $sum += $i }

What is the value of $sum after the loop finishes?
PowerShell
$sum = 0
for ($i = 1; $i -le 4; $i++) { $sum += $i }
A4
B9
C10
D0
Attempts:
2 left
💡 Hint
Add the numbers 1 through 4 together.