0
0
PowerShellscripting~20 mins

Return values in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
AError: Missing return value
B3 4
C7
D34
Attempts:
2 left
💡 Hint
Remember that return outputs the result of the expression.
💻 Command Output
intermediate
2: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
A
Start
End
B
Start
Returned Value
C
Returned Value
End
D
Start
Returned Value
End
Attempts:
2 left
💡 Hint
Return stops the function immediately.
📝 Syntax
advanced
2: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
}
Areturn 1, 2, 3
Breturn @(1, 2, 3)
Creturn [1, 2, 3]
Dreturn {1, 2, 3}
Attempts:
2 left
💡 Hint
PowerShell can return multiple objects separated by commas.
🔧 Debug
advanced
2: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
ABecause PowerShell functions return the last output automatically, so it should work.
BBecause the expression $x * $x is not outputted or returned.
CBecause the function lacks an explicit return statement.
DBecause the function parameter is missing a type declaration.
Attempts:
2 left
💡 Hint
PowerShell functions output the last expression automatically.
🚀 Application
expert
2: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
ALess than 5
BGreater than 5
CNo output
DEqual to 5
Attempts:
2 left
💡 Hint
Check the condition that matches the input value.