Bird
0
0

Identify the error in this PowerShell code snippet:

medium📝 Debug Q6 of 15
PowerShell - Control Flow
Identify the error in this PowerShell code snippet:
if ($num -eq 10) 'Ten' elseif $num -eq 20 { 'Twenty' } else { 'Other' }
AMissing parentheses around the elseif condition
BMissing curly braces around the if block
CUsing single quotes instead of double quotes
DNo error; the code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check if statement syntax

    The if condition is correctly enclosed in parentheses.
  2. Step 2: Check elseif syntax

    The elseif condition lacks parentheses around '$num -eq 20', which is required.
  3. Step 3: Confirm other syntax

    Curly braces are present for elseif and else blocks; single quotes are valid for strings.
  4. Final Answer:

    Missing parentheses around the elseif condition -> Option A
  5. Quick Check:

    All conditions must be in parentheses [OK]
Quick Trick: Always enclose conditions in parentheses [OK]
Common Mistakes:
  • Omitting parentheses in elseif or if conditions
  • Assuming braces are optional for single statements
  • Confusing single and double quotes for strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes