Challenge - 5 Problems
PowerShell Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this PowerShell function call?
Consider the following PowerShell function and its call. What will be the output?
PowerShell
function Get-Sum { param($a, $b) return $a + $b } Get-Sum 3 4
Attempts:
2 left
💡 Hint
Remember that return outputs the result of the expression.
✗ Incorrect
The function adds two numbers and returns the sum. Calling Get-Sum 3 4 returns 7.
💻 Command Output
intermediate2:00remaining
What does this PowerShell script output?
What is the output of this script?
PowerShell
function Test-Return { Write-Output "Start" return "Returned Value" Write-Output "End" } Test-Return
Attempts:
2 left
💡 Hint
Return stops the function immediately.
✗ Incorrect
The function writes "Start", then returns "Returned Value" and stops. "End" is never printed.
📝 Syntax
advanced2:00remaining
Which option correctly returns multiple values from a PowerShell function?
You want a function to return multiple values as an array. Which code does this correctly?
PowerShell
function Get-Values { # Return multiple values here }
Attempts:
2 left
💡 Hint
PowerShell can return multiple objects separated by commas.
✗ Incorrect
Using commas returns multiple objects. @(...) creates an array but return 1,2,3 returns three objects which PowerShell collects as an array.
🔧 Debug
advanced2:00remaining
Why does this function not return the expected value?
This function is supposed to return the square of a number but returns nothing. Why?
PowerShell
function Get-Square { param($x) $x * $x } $result = Get-Square 5 Write-Output $result
Attempts:
2 left
💡 Hint
PowerShell functions output the last expression automatically.
✗ Incorrect
PowerShell functions return the output of the last expression automatically, so $x * $x is returned without needing 'return'.
🚀 Application
expert2:00remaining
What is the output of this PowerShell script with multiple return statements?
Analyze this script and select the output it produces.
PowerShell
function Test-MultiReturn { param($val) if ($val -lt 5) { return "Less than 5" } elseif ($val -eq 5) { return "Equal to 5" } return "Greater than 5" } Test-MultiReturn 5
Attempts:
2 left
💡 Hint
Check the condition that matches the input value.
✗ Incorrect
The input is 5, so the elseif condition matches and returns "Equal to 5".