Bird
0
0

What will happen if you try to cast the string 'hello' to an integer in PowerShell?

medium📝 Debug Q6 of 15
PowerShell - Variables and Data Types
What will happen if you try to cast the string 'hello' to an integer in PowerShell?
$num = [int]'hello'
AIt converts 'hello' to 0 without any error
BIt throws a runtime error because 'hello' cannot be converted to an integer
CIt converts 'hello' to an empty string
DIt converts 'hello' to a Boolean false
Step-by-Step Solution
Solution:
  1. Step 1: Understand type casting behavior

    PowerShell attempts to convert the string to the specified type.
  2. Step 2: Attempt to cast 'hello' to int

    Since 'hello' is not a numeric string, casting to [int] fails.
  3. Final Answer:

    It throws a runtime error because 'hello' cannot be converted to an integer -> Option B
  4. Quick Check:

    Try running the code; it throws an error [OK]
Quick Trick: Casting non-numeric strings to int causes errors [OK]
Common Mistakes:
  • Assuming non-numeric strings convert to 0 silently
  • Expecting casting to return null or empty string
  • Confusing casting with parsing that ignores errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes