Bird
0
0

How can you create a hash table from two arrays, $keys = @('a','b') and $values = @(1,2), pairing each key with its value?

hard📝 Application Q8 of 15
PowerShell - Variables and Data Types
How can you create a hash table from two arrays, $keys = @('a','b') and $values = @(1,2), pairing each key with its value?
A$hash = @{ $keys = $values }
B$hash = @($keys, $values)
C$hash = @{}; for ($i=0; $i -lt $keys.Length; $i++) { $hash[$keys[$i]] = $values[$i] }
D$hash = $keys + $values
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    Create a hash table pairing each key with its corresponding value from two arrays.
  2. Step 2: Analyze each option

    $hash = @{}; for ($i=0; $i -lt $keys.Length; $i++) { $hash[$keys[$i]] = $values[$i] } uses a loop to assign each key to its value correctly. Options A, B, and D do not create a proper hash table.
  3. Final Answer:

    $hash = @{}; for ($i=0; $i -lt $keys.Length; $i++) { $hash[$keys[$i]] = $values[$i] } -> Option C
  4. Quick Check:

    Use loop to assign keys and values to hash table [OK]
Quick Trick: Use a loop to assign keys and values to hash table [OK]
Common Mistakes:
  • Trying to combine arrays directly
  • Using invalid hash table syntax
  • Not looping through arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes