0
0
PowerShellscripting~20 mins

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

Choose your learning style9 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.