Bird
0
0

Why does the following PowerShell code output only a single element?

hard📝 Conceptual Q10 of 15
PowerShell - Variables and Data Types
Why does the following PowerShell code output only a single element?
$arr = @(1, 2, 3)
$arr = $arr[1]
Write-Output $arr
AArrays cannot be reassigned in PowerShell
B$arr[1] returns a single element, replacing the array
CWrite-Output only prints the last element by default
DThe array is empty after assignment
Step-by-Step Solution
Solution:
  1. Step 1: Understand array element access

    $arr[1] accesses the second element, which is a single value, not an array.
  2. Step 2: Assignment replaces the array with that single element

    Assigning $arr = $arr[1] replaces the entire array with just the element at index 1.
  3. Final Answer:

    $arr[1] returns a single element, replacing the array -> Option B
  4. Quick Check:

    Array element access returns single value [OK]
Quick Trick: Accessing element returns single value, not array [OK]
Common Mistakes:
  • Thinking $arr[1] returns an array
  • Assuming Write-Output filters output
  • Believing arrays cannot be reassigned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes