Bird
Raised Fist0
PowerShellscripting~20 mins

Object arrays in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is an object array in PowerShell?
easy
A. A type of function that returns objects
B. A single object with many properties
C. A command to create new objects
D. A variable that holds multiple objects together in a list

Solution

  1. Step 1: Understand the definition of object arrays

    An object array is a collection that holds many objects in one variable, like a list.
  2. Step 2: Compare options with the definition

    Only A variable that holds multiple objects together in a list correctly describes an object array as a variable holding multiple objects.
  3. Final Answer:

    A variable that holds multiple objects together in a list -> Option D
  4. Quick Check:

    Object array = list of objects [OK]
Hint: Remember: arrays hold many items together [OK]
Common Mistakes:
  • Confusing object arrays with single objects
  • Thinking object arrays are commands
  • Mixing arrays with functions
2. Which of the following is the correct way to create an empty object array in PowerShell?
easy
A. $array = ()
B. $array = @()
C. $array = new-object array
D. $array = {}

Solution

  1. Step 1: Recall PowerShell syntax for empty arrays

    PowerShell uses @() to create an empty array.
  2. Step 2: Check each option

    $array = @() uses @(), which is correct. $array = () uses (), which assigns $null, not an array. $array = new-object array is incorrect syntax. $array = {} uses {}, which creates a hashtable, not an array.
  3. Final Answer:

    $array = @() -> Option B
  4. Quick Check:

    Empty array syntax = @() [OK]
Hint: Use @() to create empty arrays in PowerShell [OK]
Common Mistakes:
  • Using () which assigns null, not an array
  • Confusing arrays with hashtables {}
  • Trying to use new-object for arrays
3. What will be the output of this PowerShell code?
$array = @()
$array += 'apple'
$array += 'banana'
$array += 'cherry'
$array
medium
A. apple banana cherry
B. @('apple', 'banana', 'cherry')
C. apple banana cherry
D. Error: Cannot add to array

Solution

  1. Step 1: Understand how += adds items to arrays

    Using += adds each string as a new element to the array.
  2. Step 2: Output the array variable

    When output, PowerShell prints each element on a new line, so the output is three lines: apple, banana, cherry.
  3. Final Answer:

    apple banana cherry -> Option A
  4. Quick Check:

    Array elements print line by line [OK]
Hint: Arrays print each item on its own line [OK]
Common Mistakes:
  • Expecting all items on one line separated by spaces
  • Thinking += causes an error on arrays
  • Confusing array output with string output
4. Identify the error in this PowerShell script:
$array = @()
$array += 'one'
$array = $array + 'two'
$array = $array + 3
$array
medium
A. No error; script runs and outputs all items
B. Using + operator on arrays causes error
C. Cannot add integer to string array
D. Array must be declared with fixed size

Solution

  1. Step 1: Check adding different types to array

    PowerShell arrays can hold mixed types, so adding integer 3 is allowed.
  2. Step 2: Check use of + operator on arrays

    Using + concatenates arrays or adds items; it works fine here.
  3. Final Answer:

    No error; script runs and outputs all items -> Option A
  4. Quick Check:

    PowerShell arrays accept mixed types and + works [OK]
Hint: PowerShell arrays can mix types and use + to add items [OK]
Common Mistakes:
  • Thinking arrays only hold one data type
  • Believing + operator causes errors on arrays
  • Assuming arrays need fixed size
5. You have an array of objects representing people:
$people = @(
  [pscustomobject]@{Name='Alice'; Age=30},
  [pscustomobject]@{Name='Bob'; Age=25},
  [pscustomobject]@{Name='Carol'; Age=35}
)

How do you add a new person object with Name='Dave' and Age=40 to this array?
hard
A. $people = @($people, @{Name='Dave'; Age=40})
B. $people = $people + @{Name='Dave'; Age=40}
C. $people += [pscustomobject]@{Name='Dave'; Age=40}
D. $people.Add([pscustomobject]@{Name='Dave'; Age=40})

Solution

  1. Step 1: Create a new person object correctly

    Use [pscustomobject]@{Name='Dave'; Age=40} to create the new object.
  2. Step 2: Add the new object to the existing array

    Use += to add the new object to the $people array. $people += [pscustomobject]@{Name='Dave'; Age=40} does this correctly.
  3. Final Answer:

    $people += [pscustomobject]@{Name='Dave'; Age=40} -> Option C
  4. Quick Check:

    Add object to array using += [OK]
Hint: Use += with new [pscustomobject] to add to array [OK]
Common Mistakes:
  • Forgetting [pscustomobject] and adding plain hashtable
  • Using .Add() which does not work on arrays
  • Reassigning array incorrectly causing nested arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard