Bird
0
0

Which PowerShell command correctly generates even numbers between 4 and 12 using the range operator and filtering?

hard📝 Application Q8 of 15
PowerShell - Operators
Which PowerShell command correctly generates even numbers between 4 and 12 using the range operator and filtering?
A4..12 | Where-Object { $_ % 2 -eq 0 }
B4..12 | Where-Object { $_ % 2 -ne 0 }
C2..6 | ForEach-Object { $_ * 2 }
D4..12 | ForEach-Object { $_ + 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Generate the full range

    4..12 creates numbers from 4 to 12 inclusive.
  2. Step 2: Filter even numbers

    Using Where-Object { $_ % 2 -eq 0 } filters only even numbers.
  3. Step 3: Evaluate options

    4..12 | Where-Object { $_ % 2 -eq 0 } correctly filters evens. 4..12 | Where-Object { $_ % 2 -ne 0 } filters odds. 2..6 | ForEach-Object { $_ * 2 } generates 2 to 6 and multiplies by 2, which also produces evens but from 4 to 12 indirectly. 4..12 | ForEach-Object { $_ + 2 } adds 2 to each number, not filtering evens.
  4. Final Answer:

    4..12 | Where-Object { $_ % 2 -eq 0 } -> Option A
  5. Quick Check:

    Check if filtering condition matches even numbers [OK]
Quick Trick: Filter evens with Where-Object { $_ % 2 -eq 0 } [OK]
Common Mistakes:
  • Filtering odd numbers instead of even
  • Using ForEach-Object to add instead of filter
  • Assuming multiplication always generates evens

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes