Bird
0
0

Given this code:

hard📝 Application Q9 of 15
PowerShell - Variables and Data Types
Given this code:
$data = @{ 'a' = '1'; 'b' = 'two'; 'c' = '3' }
$result = @{}
foreach ($key in $data.Keys) {
  $result[$key] = [int]$data[$key]
}
Write-Output $result

What will happen when this runs?
AValues remain strings in $result.
BAll values cast successfully; 'two' becomes 0.
CScript skips 'two' and continues.
DRuntime error when casting 'two' to int.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze casting each dictionary value

    '1' and '3' cast to int fine; 'two' cannot be cast to int.
  2. Step 2: Result of invalid cast in loop

    PowerShell throws a runtime error on casting 'two' to int, stopping execution.
  3. Final Answer:

    Runtime error when casting 'two' to int. -> Option D
  4. Quick Check:

    Invalid string cast in loop = runtime error [OK]
Quick Trick: Casting invalid strings in loops causes runtime errors [OK]
Common Mistakes:
  • Assuming invalid casts become zero
  • Expecting loop to skip errors automatically
  • Confusing string and int types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes