Bird
0
0

What will be the output of the following PowerShell code?

medium📝 Command Output Q4 of 15
PowerShell - Control Flow

What will be the output of the following PowerShell code?

for ($i=2; $i -le 4; $i++) { Write-Output ($i + 1) }
A2 3 4
B3 4 5
C1 2 3
D4 5 6
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop initialization and condition

    The loop starts with $i=2 and runs while $i is less than or equal to 4.
  2. Step 2: Calculate output for each iteration

    For each $i, output is ($i + 1): when $i=2 output 3, $i=3 output 4, $i=4 output 5.
  3. Final Answer:

    3 4 5 -> Option B
  4. Quick Check:

    Output is $i plus one for each iteration [OK]
Quick Trick: Add 1 to loop variable for output [OK]
Common Mistakes:
  • Confusing loop variable with output
  • Misreading loop condition
  • Forgetting increment step

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes