Bird
Raised Fist0
PowerShellscripting~20 mins

Type casting in PowerShell - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does type casting do in PowerShell?
easy
A. It changes a value from one data type to another.
B. It creates a new variable without a value.
C. It deletes a variable from memory.
D. It runs a script automatically.

Solution

  1. Step 1: Understand type casting meaning

    Type casting means converting a value from one type to another, like string to integer.
  2. Step 2: Match with options

    Only It changes a value from one data type to another. describes changing data types, which is type casting.
  3. Final Answer:

    It changes a value from one data type to another. -> Option A
  4. Quick Check:

    Type casting = changing data type [OK]
Hint: Type casting means changing data type of a value [OK]
Common Mistakes:
  • Confusing type casting with variable creation
  • Thinking it deletes variables
  • Assuming it runs scripts automatically
2. Which of the following is the correct syntax to cast the string '123' to an integer in PowerShell?
easy
A. [int]'123'
B. int('123')
C. (int)'123'
D. cast int '123'

Solution

  1. Step 1: Recall PowerShell casting syntax

    PowerShell uses [Type](value) to cast, e.g., [int]'123'.
  2. Step 2: Check options for correct syntax

    [int]'123' matches the correct syntax. Options A, C, and D are invalid in PowerShell.
  3. Final Answer:

    [int]'123' -> Option A
  4. Quick Check:

    Use [Type](value) for casting [OK]
Hint: Use square brackets with type before value [OK]
Common Mistakes:
  • Using parentheses like a function call
  • Trying to use 'cast' keyword
  • Missing square brackets
3. What will be the output of this PowerShell code?
$value = [int]'5.7'
Write-Output $value
medium
A. 5.7
B. 5
C. Error
D. 6

Solution

  1. Step 1: Understand casting string '5.7' to int

    Casting string '5.7' to int truncates the decimal part, resulting in 5.
  2. Step 2: Check output of Write-Output

    It prints the integer 5, not 5.7 or 6, and no error occurs.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    String to int truncates decimals = 5 [OK]
Hint: Casting float string to int truncates decimals [OK]
Common Mistakes:
  • Expecting rounding instead of truncation
  • Thinking it throws an error
  • Assuming output stays as string
4. Identify the error in this PowerShell code:
$num = [int] 'abc'
medium
A. No error, $num becomes 0
B. Syntax error due to space between [int] and string
C. Runtime error: Cannot convert 'abc' to int
D. It converts 'abc' to string automatically

Solution

  1. Step 1: Analyze casting 'abc' to int

    String 'abc' cannot be converted to integer, causing a runtime error.
  2. Step 2: Check syntax and error type

    Space between [int] and string is allowed; error is runtime conversion failure.
  3. Final Answer:

    Runtime error: Cannot convert 'abc' to int -> Option C
  4. Quick Check:

    Invalid string to int causes runtime error [OK]
Hint: Invalid string to int causes runtime error [OK]
Common Mistakes:
  • Thinking space causes syntax error
  • Assuming it converts to zero
  • Believing it auto-converts to string
5. You have a list of strings representing numbers: @('10', '20', '30'). How do you convert all items to integers in PowerShell efficiently?
hard
A. @('10', '20', '30').ToInt()
B. [int]@('10', '20', '30')
C. Cast-Int @('10', '20', '30')
D. @('10', '20', '30') | ForEach-Object { [int]$_ }

Solution

  1. Step 1: Understand list conversion

    Each string must be cast individually to int; PowerShell does not cast arrays directly.
  2. Step 2: Check options for correct method

    @('10', '20', '30') | ForEach-Object { [int]$_ } uses ForEach-Object to cast each item correctly. Options A, B, and D are invalid or unsupported.
  3. Final Answer:

    @('10', '20', '30') | ForEach-Object { [int]$_ } -> Option D
  4. Quick Check:

    Use ForEach-Object to cast each item [OK]
Hint: Use ForEach-Object to cast each array item [OK]
Common Mistakes:
  • Trying to cast whole array at once
  • Using non-existent methods like ToInt()
  • Assuming a Cast-Int cmdlet exists
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