Bird
0
0

You have an object array $arr containing mixed types: strings, numbers, and booleans. How can you create a new array containing only the string elements?

hard📝 Application Q8 of 15
PowerShell - Working with Objects
You have an object array $arr containing mixed types: strings, numbers, and booleans. How can you create a new array containing only the string elements?
A$arr | Where-Object { $_ -like '*string*' }
B$arr | Where-Object { $_ -eq 'string' }
C$arr | Where-Object { $_.GetType() -eq 'string' }
D$arr | Where-Object { $_ -is [string] }
Step-by-Step Solution
Solution:
  1. Step 1: Filter array by type

    Use Where-Object with -is operator to check if element is string type.
  2. Step 2: Evaluate options

    $arr | Where-Object { $_ -is [string] } correctly filters strings. Others compare values or types incorrectly.
  3. Final Answer:

    $arr | Where-Object { $_ -is [string] } -> Option D
  4. Quick Check:

    Use -is to filter by type = D [OK]
Quick Trick: Use -is operator to filter by object type [OK]
Common Mistakes:
  • Using -eq to compare type as string
  • Using GetType() without proper comparison
  • Using -like which matches strings, not types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes