Bird
0
0

Identify the error in this PowerShell code snippet:

medium📝 Debug Q14 of 15
PowerShell - Variables and Data Types
Identify the error in this PowerShell code snippet:
$ht = @{ 'x' = 10 }
$ht.Add('x', 20)
ARuntime error: key 'x' already exists
BSyntax error in hash table creation
CNo error, code runs fine
DAdd method does not exist for hash tables
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash table initialization

    The hash table $ht is created with key 'x' = 10.
  2. Step 2: Analyze the Add() method call

    Calling $ht.Add('x', 20) tries to add a duplicate key 'x', which causes a runtime error.
  3. Final Answer:

    Runtime error: key 'x' already exists -> Option A
  4. Quick Check:

    Adding duplicate key causes runtime error [OK]
Quick Trick: Add() fails if key already exists in hash table [OK]
Common Mistakes:
  • Assuming Add() updates existing keys
  • Thinking Add() method doesn't exist
  • Confusing syntax error with runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes