Bird
0
0

You want a PowerShell function Calculate-Sum that adds two numbers, but if the second number is not provided, it should default to the first number. Which parameter default setup achieves this?

hard📝 Application Q15 of 15
PowerShell - Functions
You want a PowerShell function Calculate-Sum that adds two numbers, but if the second number is not provided, it should default to the first number. Which parameter default setup achieves this?
Aparam([int]$a, [int]$b = 0)
Bparam([int]$a = 0, [int]$b = 0)
Cparam([int]$a, [int]$b = $null)
Dparam([int]$a, [int]$b = $a)
Step-by-Step Solution
Solution:
  1. Step 1: Understand PowerShell parameter default evaluation

    PowerShell binds earlier parameters before evaluating defaults for later ones, allowing references to prior parameters.
  2. Step 2: Identify the correct setup

    param([int]$a, [int]$b = $a) sets $b to the value of $a when $b is not provided.
  3. Final Answer:

    param([int]$a, [int]$b = $a) -> Option D
  4. Quick Check:

    Later defaults can reference earlier parameters [OK]
Quick Trick: Later parameters can default using earlier parameter values [OK]
Common Mistakes:
  • Using fixed defaults like 0 or $null
  • Thinking parameter references are not allowed in defaults
  • Assuming defaults must be literal constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes