0
0
PowerShellscripting~20 mins

Type casting in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Type Casting 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 type casting?
Consider the following PowerShell code snippet. What will be the output?
PowerShell
$a = [int]"123"
$b = [string]42
$c = [bool]0
Write-Output "$a,$b,$c"
A123,42,False
B123,42,True
C123,42,0
D123,42,1
Attempts:
2 left
💡 Hint
Remember how PowerShell converts strings to integers and numbers to booleans.
📝 Syntax
intermediate
2:00remaining
Which option correctly casts a string to a datetime in PowerShell?
You want to convert the string "2024-06-15" to a DateTime object. Which option does this correctly?
A[date]"2024-06-15"
BConvertTo-DateTime "2024-06-15"
C[datetime]"2024-06-15"
DCast-DateTime "2024-06-15"
Attempts:
2 left
💡 Hint
PowerShell uses square brackets for type casting.
🔧 Debug
advanced
2:00remaining
Why does this PowerShell cast fail with an error?
Examine the code below and identify why it causes an error.
PowerShell
$num = [int]"abc"
Write-Output $num
ABecause "abc" cannot be converted to an integer, causing a runtime error.
BBecause the variable $num is not declared before casting.
CBecause Write-Output cannot output variables cast from strings.
DBecause the syntax for casting is incorrect; it should be (int)"abc".
Attempts:
2 left
💡 Hint
Think about what happens when you try to convert non-numeric text to a number.
🚀 Application
advanced
2:00remaining
How many items are in the resulting array after this cast?
What is the count of elements in the array after this code runs?
PowerShell
$input = "1,2,3,4"
$array = [int[]]($input -split ',')
$array.Count
A1
B4
C0
DError
Attempts:
2 left
💡 Hint
Splitting the string creates multiple strings, then casting to int array converts each element.
🧠 Conceptual
expert
2:00remaining
What is the value and type of $result after this cast?
Given the code below, what is the value and type of $result?
PowerShell
$result = [bool][int]"0"
A$result is "False" (String)
B$result is True (Boolean)
C$result is 0 (Integer)
D$result is False (Boolean)
Attempts:
2 left
💡 Hint
Casting happens inside out: string to int, then int to bool.