An object array in PowerShell is a collection that holds multiple objects, allowing you to store and manage several items together in one variable.
Click to reveal answer
beginner
How do you create an empty object array in PowerShell?
You create an empty object array by assigning an empty array literal: $array = @().
Click to reveal answer
beginner
How can you add an object to an existing array in PowerShell?
You can add an object by using the += operator, like $array += $newObject. This appends the new object to the array.
Click to reveal answer
intermediate
What is the difference between an array and an object array in PowerShell?
An array can hold any type of data, but an object array specifically holds objects, which can have properties and methods, making them more versatile.
Click to reveal answer
beginner
How do you access the properties of objects inside an object array?
You access properties by indexing the array and then using dot notation, for example: $array[0].PropertyName.
Click to reveal answer
How do you initialize an empty object array in PowerShell?
A$array = {}
B$array = @()
C$array = []
D$array = ()
✗ Incorrect
In PowerShell, @() creates an empty array. Curly braces {} are for script blocks, square brackets [] are for type literals, and parentheses () are for grouping.
Which operator adds an object to an existing array in PowerShell?
A/=
B-=
C*=
D+=
✗ Incorrect
The += operator appends an item to an array in PowerShell.
How do you access the first object's property named 'Name' in an object array called $arr?
A$arr[0].Name
B$arr.Name[0]
C$arr->Name[0]
D$arr[Name].0
✗ Incorrect
Use index first, then dot notation: $arr[0].Name.
What type of elements can an object array hold in PowerShell?
AAny objects including custom objects
BOnly numbers
COnly integers
DOnly strings
✗ Incorrect
Object arrays can hold any objects, including custom objects with properties.
What happens if you add an object to an array using += in PowerShell?
AThe array is cleared
BThe object replaces the array
CThe object is appended to the array
DAn error occurs
✗ Incorrect
Using += appends the new object to the existing array.
Explain how to create and add objects to an object array in PowerShell.
Think about starting empty and then adding items one by one.
You got /3 concepts.
Describe how to access properties of objects stored inside an object array.
Remember arrays use numbers to pick items, objects use names for properties.
You got /3 concepts.
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
Step 1: Understand the definition of object arrays
An object array is a collection that holds many objects in one variable, like a list.
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.
Final Answer:
A variable that holds multiple objects together in a list -> Option D
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
Step 1: Recall PowerShell syntax for empty arrays
PowerShell uses @() to create an empty array.
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.
Final Answer:
$array = @() -> Option B
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?