A. Casting applies only to $x, division happens after, causing unexpected result
B. Division operator is invalid between int and float
C. Casting $x to int is unnecessary but valid
D. Write-Output cannot print variables
Solution
Step 1: Analyze casting and operation order
[int]$x casts 10 to int (already int), then division by 3.5 happens.
Step 2: Understand result type and unexpected behavior
Division happens after casting, so $z is float 2.857..., not int as might be expected.
Final Answer:
Casting applies only to $x, division happens after, causing unexpected result -> Option A
Quick Check:
Casting affects only $x, division result is float [OK]
Hint: Casting affects only immediate value, not whole expression [OK]
Common Mistakes:
Thinking casting applies to entire expression
Believing division fails between int and float
Assuming Write-Output can't print variables
5. You want to calculate the average of three numbers: 4, 7, and 10 in PowerShell, ensuring the result is a floating-point number. Which script correctly does this?
hard
A.
$avg = ([int](4 + 7 + 10)) / 3 Write-Output $avg
B.
$avg = (4 + 7 + 10) / [int]3 Write-Output $avg
C.
$avg = [int](4 + 7 + 10 / 3) Write-Output $avg
D.
$avg = (4 + 7 + 10) / 3.0 Write-Output $avg
Solution
Step 1: Understand how to get floating-point average
Dividing by a floating-point number (3.0) forces floating-point division.
Step 2: Check each option's effect
$avg = ([int](4 + 7 + 10)) / 3 Write-Output $avg
casts sum to int (already int), division by int 3 gives integer division result truncated.
$avg = [int](4 + 7 + 10 / 3) Write-Output $avg
casts after dividing 10 by 3, changing order and result.