0
0
PowerShellscripting~20 mins

Object arrays in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Object Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of creating and accessing object arrays
What is the output of this PowerShell script?
PowerShell
$arr = @()
$arr += [PSCustomObject]@{Name='Alice'; Age=30}
$arr += [PSCustomObject]@{Name='Bob'; Age=25}
$arr[1].Name
A30
BAlice
C25
DBob
Attempts:
2 left
💡 Hint
Remember arrays start at index 0.
📝 Syntax
intermediate
2:00remaining
Correct syntax to create an array of objects
Which option correctly creates an array of two objects with properties Name and Age?
A$arr = @([PSCustomObject]([Name='Tom'; Age=40]), [PSCustomObject]([Name='Jerry'; Age=35]))
B$arr = @([PSCustomObject]@{Name='Tom'; Age=40}, [PSCustomObject]@{Name='Jerry'; Age=35})
C$arr = @([PSCustomObject]{Name='Tom'; Age=40}, [PSCustomObject]{Name='Jerry'; Age=35})
D$arr = [PSCustomObject]@{Name='Tom'; Age=40}, [PSCustomObject]@{Name='Jerry'; Age=35}
Attempts:
2 left
💡 Hint
Use @() to create an array and @{} to create objects.
🔧 Debug
advanced
2:00remaining
Identify the error in adding objects to an array
What error does this script produce? $arr = @() $arr += @{Name='Anna'; Age=28} $arr[0].Name
ANo error, outputs 'Anna'
BRuntime error: Cannot index into a null array
CRuntime error: Property 'Name' not found
DSyntax error: Missing [PSCustomObject]
Attempts:
2 left
💡 Hint
Check the type of object added to the array.
🚀 Application
advanced
2:00remaining
Filter object array by property value
Given an array of objects with property Age, which command returns only objects where Age is greater than 30?
A$arr | Where-Object { $_.Age -gt 30 }
B$arr | Where-Object Age > 30
C$arr | Where { Age -gt 30 }
D$arr | Filter-Object { $_.Age -gt 30 }
Attempts:
2 left
💡 Hint
Use Where-Object with a script block and $_ for current object.
🧠 Conceptual
expert
2:00remaining
Understanding object array property modification
If you modify a property of an object inside a PowerShell array, what happens to the array?
AThe array updates because objects are referenced, so changes reflect in the array.
BThe array remains unchanged because objects are copied when added.
CThe array throws an error because objects are immutable.
DThe array duplicates the object with the new property value.
Attempts:
2 left
💡 Hint
Think about how PowerShell handles objects in arrays.