Bird
Raised Fist0
PowerShellscripting~20 mins

Integer and floating-point types in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PowerShell Number Master
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 code?
Consider the following PowerShell script:
$a = 5
$b = 2.5
$c = $a + $b
Write-Output $c.GetType().Name
What will be printed?
PowerShell
$a = 5
$b = 2.5
$c = $a + $b
Write-Output $c.GetType().Name
AString
BInt32
CDecimal
DDouble
Attempts:
2 left
💡 Hint
Adding an integer and a floating-point number results in a floating-point type.
🧠 Conceptual
intermediate
2:00remaining
Which type does PowerShell assign to the number 3.0 by default?
In PowerShell, if you assign the value 3.0 to a variable like this:
$x = 3.0
What is the default type of $x?
ADouble
BInt32
CSingle
DDecimal
Attempts:
2 left
💡 Hint
Numbers with decimal points default to a floating-point type.
📝 Syntax
advanced
2:00remaining
Which PowerShell assignment causes a type conversion error?
Which of the following assignments will cause a runtime error in PowerShell due to type mismatch?
A$z = [double] 5
B$x = [int] 3.7
C$y = [int] 'abc'
D$w = [float] 2.5
Attempts:
2 left
💡 Hint
Converting a non-numeric string to int causes an error.
🚀 Application
advanced
2:00remaining
How many items are in the resulting array after this PowerShell script?
What is the length of the array $arr after running this script?
$arr = @()
for ($i = 0; $i -lt 5; $i++) {
if ($i -is [int]) { $arr += $i }
else { $arr += [math]::Round($i) }
}
A5
B10
C0
D1
Attempts:
2 left
💡 Hint
The loop runs 5 times and adds one item each time.
🔧 Debug
expert
2:00remaining
What error does this PowerShell code raise?
Examine this code snippet:
$num = 1.5
$intNum = [int]$num
$result = $intNum / 0

What error will occur when running this code?
ANullReferenceException
BDivideByZeroException
CNo error, outputs Infinity
DInvalidCastException
Attempts:
2 left
💡 Hint
Dividing by zero causes an error in integer division.

Practice

(1/5)
1. Which data type in PowerShell is used to store whole numbers without decimals?
easy
A. Boolean
B. String
C. Integer
D. Floating-point

Solution

  1. Step 1: Understand data types for numbers

    Integers store whole numbers without any decimal part.
  2. Step 2: Identify the correct type for whole numbers

    Floating-point types store decimals, strings store text, booleans store true/false values.
  3. Final Answer:

    Integer -> Option C
  4. Quick Check:

    Whole numbers use Integer type [OK]
Hint: Whole numbers = Integer type, decimals = Floating-point [OK]
Common Mistakes:
  • Confusing Integer with Floating-point
  • Choosing String for numbers
  • Using Boolean for numeric values
2. Which of the following is the correct way to declare a floating-point number in PowerShell?
easy
A. $num = 5.0
B. $num = [int]5.0
C. $num = '5.0'
D. $num = 5

Solution

  1. Step 1: Recognize floating-point syntax

    Floating-point numbers include a decimal point, like 5.0.
  2. Step 2: Check each option

    A: $num = 5 is an integer, B: $num = [int]5.0 casts to integer removing decimals, C: $num = '5.0' is a string, D: $num = 5.0 is floating-point.
  3. Final Answer:

    $num = 5.0 -> Option A
  4. Quick Check:

    Decimal number assigned directly = $num = 5.0 [OK]
Hint: Decimals need a dot, no quotes for numbers [OK]
Common Mistakes:
  • Using quotes around numbers
  • Casting float to int unintentionally
  • Assigning integer without decimal
3. What will be the output of this PowerShell code?
$a = 7
$b = 2
$c = $a / $b
Write-Output $c
medium
A. 3
B. 3.5
C. 3.0
D. Error

Solution

  1. Step 1: Understand division with integers in PowerShell

    PowerShell automatically converts to floating-point when dividing integers if result is not whole.
  2. Step 2: Calculate 7 divided by 2

    7 / 2 = 3.5, a floating-point number.
  3. Final Answer:

    3.5 -> Option B
  4. Quick Check:

    Integer division returns float if needed = 3.5 [OK]
Hint: Division returns float if not exact integer [OK]
Common Mistakes:
  • Expecting integer truncation
  • Thinking division causes error
  • Confusing 3.0 with 3.5
4. Identify the error in this PowerShell script:
$x = 10
$y = 3.5
$z = [int]$x / $y
Write-Output $z
medium
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

  1. Step 1: Analyze casting and operation order

    [int]$x casts 10 to int (already int), then division by 3.5 happens.
  2. Step 2: Understand result type and unexpected behavior

    Division happens after casting, so $z is float 2.857..., not int as might be expected.
  3. Final Answer:

    Casting applies only to $x, division happens after, causing unexpected result -> Option A
  4. 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

  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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard