Bird
0
0

What will be the output of this PowerShell code?

medium📝 Predict Output Q5 of 15
PowerShell - Working with Objects
What will be the output of this PowerShell code?
$obj = New-Object PSObject
$obj | Add-Member -MemberType ScriptMethod -Name Multiply -Value { param($x, $y) $x * $y }
$obj.Multiply(3, 4)
AError: Missing parameters
B7
C12
D3 4
Step-by-Step Solution
Solution:
  1. Step 1: Analyze ScriptMethod with parameters

    The ScriptMethod Multiply takes two parameters $x and $y and returns their product.
  2. Step 2: Calculate method call result

    Calling $obj.Multiply(3, 4) returns 3 * 4 = 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    ScriptMethod with params returns product = A [OK]
Quick Trick: Define parameters in script block to accept arguments [OK]
Common Mistakes:
  • Assuming parameters are not supported
  • Expecting sum instead of product
  • Confusing output with string concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes