Bird
0
0

Why does this code output 'No'?

hard📝 Conceptual Q10 of 15
PowerShell - Variables and Data Types
Why does this code output 'No'?
$x = 0
if ($x) { "Yes" } else { "No" }
A0 is treated as $true in PowerShell conditionals
B0 is treated as $false in PowerShell conditionals
CVariables must be Boolean type to be used in if
DThe code has a syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand how PowerShell treats numbers in conditions

    In PowerShell, 0 is treated as $false in conditional statements.
  2. Step 2: Evaluate the if condition

    $x is 0, so 'if ($x)' evaluates to false, so the else block runs and outputs 'No'.
  3. Final Answer:

    0 is treated as $false in PowerShell conditionals -> Option B
  4. Quick Check:

    0 equals false in if conditions [OK]
Quick Trick: 0 is false in PowerShell if conditions [OK]
Common Mistakes:
  • Assuming 0 is true like in some languages
  • Thinking variables must be Boolean type in if
  • Believing code has syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes