Bird
0
0

What will be the output of this PowerShell code?

medium📝 Command Output Q13 of 15
PowerShell - Control Flow
What will be the output of this PowerShell code?
switch -Regex ('apple', 'banana', 'cherry') {
  '^a' { 'Starts with a' }
  'an' { 'Contains an' }
  default { 'No match' }
}
AStarts with a Contains an No match
BStarts with a No match No match
CNo match Contains an No match
DStarts with a Contains an Contains an
Step-by-Step Solution
Solution:
  1. Step 1: Check each input against regex patterns

    'apple' matches '^a' (starts with 'a'), so outputs 'Starts with a'. 'banana' contains 'an', so matches 'an' pattern, outputs 'Contains an'. 'cherry' matches none, so outputs 'No match'.
  2. Step 2: Confirm output order

    The outputs correspond to each input in order: 'apple' -> 'Starts with a', 'banana' -> 'Contains an', 'cherry' -> 'No match'.
  3. Final Answer:

    Starts with a Contains an No match -> Option A
  4. Quick Check:

    Regex matches each input correctly = Starts with a Contains an No match [OK]
Quick Trick: Match each input line by line with regex patterns [OK]
Common Mistakes:
  • Assuming only first match per input
  • Confusing regex anchors like ^
  • Ignoring default case output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes