Bird
0
0

What is wrong with this PowerShell code?

medium📝 Debug Q7 of 15
PowerShell - Working with Objects
What is wrong with this PowerShell code?
$arr = @()
$arr = $arr + 'item'
ANo error; this correctly adds 'item' to the array
BYou cannot use + operator with arrays
CThe array must be declared as [object[]]
DThe variable $arr is read-only
Step-by-Step Solution
Solution:
  1. Step 1: Understand array concatenation

    Using + with arrays concatenates and returns a new array.
  2. Step 2: Check code behavior

    $arr + 'item' adds 'item' to array; assigning back to $arr updates it correctly.
  3. Final Answer:

    No error; this correctly adds 'item' to the array -> Option A
  4. Quick Check:

    + operator concatenates arrays = A [OK]
Quick Trick: Use + to concatenate arrays and assign back [OK]
Common Mistakes:
  • Thinking + cannot be used with arrays
  • Not assigning result back to variable
  • Confusing with list methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes