Bird
0
0

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📝 Application Q15 of 15
PowerShell - Variables and Data Types
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?
A<pre>$avg = ([int](4 + 7 + 10)) / 3<br>Write-Output $avg</pre>
B<pre>$avg = (4 + 7 + 10) / [int]3<br>Write-Output $avg</pre>
C<pre>$avg = [int](4 + 7 + 10 / 3)<br>Write-Output $avg</pre>
D<pre>$avg = (4 + 7 + 10) / 3.0<br>Write-Output $avg</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to get floating-point average

    Dividing by a floating-point number (3.0) forces floating-point division.
  2. 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.
    $avg = (4 + 7 + 10) / [int]3
    Write-Output $avg
    divides by int 3, integer division.
    $avg = (4 + 7 + 10) / 3.0
    Write-Output $avg
    divides by 3.0, ensuring float result.
  3. Final Answer:

    $avg = (4 + 7 + 10) / 3.0
    Write-Output $avg
    -> Option D
  4. Quick Check:

    Divide by float to get float average [OK]
Quick Trick: Divide by float (3.0) to get float result [OK]
Common Mistakes:
  • Casting sum to int before division
  • Dividing by int causing integer division
  • Incorrect operator precedence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes