Bird
0
0

Why does PowerShell allow casting a non-zero integer to [bool] as true internally?

hard📝 Conceptual Q10 of 15
PowerShell - Variables and Data Types
Why does PowerShell allow casting a non-zero integer to [bool] as true internally?
ABecause PowerShell converts all integers to strings first.
BBecause only 1 is true; others cause errors.
CBecause any non-zero value is considered logically true in Boolean context.
DBecause casting to Boolean always returns false.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Boolean casting rules

    PowerShell treats any non-zero integer as logically true when cast to Boolean.
  2. Step 2: Check other options

    The other options do not correctly describe PowerShell's Boolean casting behavior.
  3. Final Answer:

    Any non-zero value is considered logically true in Boolean context. -> Option C
  4. Quick Check:

    Non-zero int to bool = true [OK]
Quick Trick: Non-zero integers cast to true in Boolean context [OK]
Common Mistakes:
  • Thinking only 1 is true
  • Assuming casting converts to string first
  • Believing casting to bool returns false always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes