Bird
Raised Fist0
PowerShellscripting~20 mins

Hash tables (dictionaries) in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell hash table access?

Consider the following PowerShell script:

$hash = @{ 'apple' = 3; 'banana' = 5; 'cherry' = 7 }
$result = $hash['banana']
Write-Output $result

What will be printed?

PowerShell
$hash = @{ 'apple' = 3; 'banana' = 5; 'cherry' = 7 }
$result = $hash['banana']
Write-Output $result
A7
B3
C5
Dbanana
Attempts:
2 left
💡 Hint

Think about how to get the value for a key in a hash table.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a hash table with keys 'x' and 'y' and values 10 and 20?

Choose the correct PowerShell syntax to create a hash table with keys 'x' and 'y' and values 10 and 20 respectively.

A@{ 'x' : 10, 'y' : 20 }
B@{ x = 10; y = 20 }
C@{ 'x' = 10, 'y' = 20 }
D@{ x : 10; y : 20 }
Attempts:
2 left
💡 Hint

PowerShell uses = to assign values in hash tables, and semicolons separate pairs.

🔧 Debug
advanced
2:00remaining
Why does this PowerShell hash table script cause an error?

Look at this script:

$hash = @{ 'a' = 1; 'b' = 2 }
Write-Output $hash[a]

Why does it cause an error?

ABecause 'a' is not quoted when accessing the hash table key
BBecause the hash table keys must be integers
CBecause Write-Output cannot print hash table values
DBecause the hash table is empty
Attempts:
2 left
💡 Hint

Think about how keys are referenced in PowerShell hash tables.

🚀 Application
advanced
2:00remaining
How to add a new key-value pair to an existing PowerShell hash table?

You have this hash table:

$hash = @{ 'name' = 'Alice'; 'age' = 30 }

Which command correctly adds a new key 'city' with value 'Seattle'?

A$hash.Add('city', 'Seattle')
BAdd-Item $hash 'city' 'Seattle'
C$hash += @{ 'city' = 'Seattle' }
D$hash['city'] = 'Seattle'
Attempts:
2 left
💡 Hint

Think about how to assign a new key in a hash table.

🧠 Conceptual
expert
2:00remaining
What is the count of items in this PowerShell hash table after execution?

Consider this script:

$hash = @{ 'one' = 1; 'two' = 2 }
$hash['three'] = 3
$hash['two'] = 22
$hash.Remove('one')

How many key-value pairs remain in $hash?

A2
B3
C1
D4
Attempts:
2 left
💡 Hint

Count how many keys remain after adding, updating, and removing.

Practice

(1/5)
1. What is the correct way to create an empty hash table in PowerShell?
easy
A. ()
B. []
C. @{}
D. {}

Solution

  1. Step 1: Understand hash table syntax in PowerShell

    PowerShell uses @{} to define hash tables, which store key-value pairs.
  2. Step 2: Identify the empty hash table syntax

    An empty hash table has no key-value pairs inside, so it is @{}.
  3. Final Answer:

    @{} -> Option C
  4. Quick Check:

    Empty hash table = @{ } [OK]
Hint: Use @{} to create hash tables in PowerShell [OK]
Common Mistakes:
  • Using {} which is a script block, not a hash table
  • Using [] which is an array, not a hash table
  • Using () which is for grouping or method calls
2. Which of the following is the correct syntax to add a key-value pair 'Name' = 'Alice' to an existing hash table $person?
easy
A. $person[Name] = 'Alice'
B. $person.Name = Alice
C. $person->Add('Name', 'Alice')
D. $person.Add('Name', 'Alice')

Solution

  1. Step 1: Recall how to add key-value pairs to hash tables

    PowerShell hash tables have an Add() method to add new keys and values.
  2. Step 2: Identify correct method call syntax

    The correct syntax is $person.Add('Name', 'Alice'). Note that the Add() method specifically adds new entries.
  3. Final Answer:

    $person.Add('Name', 'Alice') -> Option D
  4. Quick Check:

    Add key-value with Add() method [OK]
Hint: Use Add() method to add new keys to hash tables [OK]
Common Mistakes:
  • Using -> which is not PowerShell syntax
  • Using dot notation with unquoted value
  • Using unquoted key in square brackets
3. What will be the output of this PowerShell code?
$hash = @{ 'a' = 1; 'b' = 2 }
$hash['a'] = 3
$hash['c'] = 4
$hash['b']
medium
A. 2
B. 1
C. 3
D. 4

Solution

  1. Step 1: Understand the initial hash table and updates

    The hash table starts with keys 'a' = 1 and 'b' = 2. Then 'a' is updated to 3, and a new key 'c' = 4 is added.
  2. Step 2: Identify the value accessed

    The code outputs $hash['b'], which was never changed and remains 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Access unchanged key 'b' = 2 [OK]
Hint: Check which key is accessed after updates [OK]
Common Mistakes:
  • Confusing updated key 'a' with accessed key 'b'
  • Assuming last added key 'c' is accessed
  • Mixing values of different keys
4. Identify the error in this PowerShell code snippet:
$ht = @{ 'x' = 10 }
$ht.Add('x', 20)
medium
A. Runtime error: key 'x' already exists
B. Syntax error in hash table creation
C. No error, code runs fine
D. Add method does not exist for hash tables

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]
Hint: 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
5. You have a list of users and their ages:
$users = @('Alice', 'Bob', 'Charlie')
$ages = @(25, 30, 35)

Which PowerShell code correctly creates a hash table mapping each user to their age?
hard
A. $userAges = @{}; foreach ($user in $users) { $userAges.Add($user, $ages) }
B. $userAges = @{}; for ($i=0; $i -lt $users.Count; $i++) { $userAges[$users[$i]] = $ages[$i] }
C. $userAges = @{}; $users | ForEach-Object { $userAges.Add($_, $ages[$_]) }
D. $userAges = @{}; for ($i=0; $i -le $users.Count; $i++) { $userAges[$i] = $ages[$i] }

Solution

  1. Step 1: Understand the goal

    Create a hash table where each user name is a key and their corresponding age is the value.
  2. Step 2: Analyze each option

    $userAges = @{}; for ($i=0; $i -lt $users.Count; $i++) { $userAges[$users[$i]] = $ages[$i] } uses a for loop with index to assign each user key to the correct age value. $userAges = @{}; foreach ($user in $users) { $userAges.Add($user, $ages) } tries to add all ages at once, which is incorrect. $userAges = @{}; $users | ForEach-Object { $userAges.Add($_, $ages[$_]) } uses $_ as index but $_ is the user string, not an index. $userAges = @{}; for ($i=0; $i -le $users.Count; $i++) { $userAges[$i] = $ages[$i] } loops one too many times and uses numeric keys instead of user names.
  3. Final Answer:

    $userAges = @{}; for ($i=0; $i -lt $users.Count; $i++) { $userAges[$users[$i]] = $ages[$i] } -> Option B
  4. Quick Check:

    Use indexed loop to map keys to values [OK]
Hint: Use index loop to map two lists into hash table [OK]
Common Mistakes:
  • Using foreach without index to access parallel array
  • Using wrong loop bounds causing errors
  • Assigning numeric keys instead of user names
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