Bird
0
0

Which of these is the correct syntax to define a PowerShell function with a string parameter named 'name' that defaults to 'Guest'?

easy📝 Syntax Q3 of 15
PowerShell - Functions
Which of these is the correct syntax to define a PowerShell function with a string parameter named 'name' that defaults to 'Guest'?
Afunction SayHello { param([string]$name <- 'Guest') Write-Output "Hello, $name" }
Bfunction SayHello { param([string]$name : 'Guest') Write-Output "Hello, $name" }
Cfunction SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" }
Dfunction SayHello { param([string]$name => 'Guest') Write-Output "Hello, $name" }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct default parameter syntax

    PowerShell uses '=' to assign default values inside param blocks.
  2. Step 2: Verify function syntax

    function SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" } correctly uses '=' and quotes for string default value.
  3. Final Answer:

    function SayHello { param([string]$name = 'Guest') Write-Output "Hello, $name" } -> Option C
  4. Quick Check:

    Default string parameter syntax = '=' with quotes [OK]
Quick Trick: Use '=' and quotes for string defaults in param [OK]
Common Mistakes:
  • Using ':' or '=>' instead of '='
  • Missing quotes around string default
  • Incorrect param block syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes