Bird
0
0

You want to create a list of even numbers from 2 to 10 using the range operator and filtering. Which script correctly does this?

hard📝 Application Q15 of 15
PowerShell - Operators
You want to create a list of even numbers from 2 to 10 using the range operator and filtering. Which script correctly does this?
A2..10 | ForEach-Object { $_ * 2 }
B2..10 | Where-Object { $_ % 2 -eq 0 }
C1..5 | Where-Object { $_ % 2 -eq 0 }
D2..10..2
Step-by-Step Solution
Solution:
  1. Step 1: Understand the range and filtering

    The range operator 2..10 creates numbers 2 to 10. Filtering with Where-Object { $_ % 2 -eq 0 } keeps only even numbers.
  2. Step 2: Check each option

    2..10 | Where-Object { $_ % 2 -eq 0 } correctly filters evens from 2 to 10. 2..10 | ForEach-Object { $_ * 2 } doubles numbers 2 to 10 (wrong). 1..5 | Where-Object { $_ % 2 -eq 0 } filters evens from 1 to 5 (wrong range). 2..10..2 uses invalid syntax (range operator does not support step).
  3. Final Answer:

    2..10 | Where-Object { $_ % 2 -eq 0 } -> Option B
  4. Quick Check:

    Filter evens from range with Where-Object [OK]
Quick Trick: Use Where-Object to filter even numbers after range [OK]
Common Mistakes:
  • Trying to add step in range operator
  • Using wrong range limits
  • Multiplying instead of filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes