A. The function does not use the return keyword to send back the sum.
B. The parameters are not declared correctly.
C. The addition operator '+' is invalid in PowerShell.
D. The variable $sum is not assigned properly.
Solution
Step 1: Check how the function returns the result
The function uses Write-Host to display the sum but does not use return to send the value back to the caller.
Step 2: Understand the difference between output and return
Write-Host sends output to the pipeline but does not return a value from the function in the expected way for assignment.
Final Answer:
The function does not use the return keyword to send back the sum. -> Option A
Quick Check:
Return keyword needed to send value back [OK]
Hint: Use return to send values back, not just Write-Host [OK]
Common Mistakes:
Confusing Write-Host with return
Thinking parameters are wrong
Believing '+' is invalid operator
5. You want to create a PowerShell function that returns the first non-empty string from a list of inputs. Which function correctly returns the first non-empty string?
hard
A. function Get-FirstNonEmpty {
param([string[]]$inputs)
return $inputs[0]
}
B. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item -eq '') { return $item }
}
return $null
}
C. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if (-not $item) { return $item }
}
return 'empty'
}
D. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item) { return $item }
}
return ''
}
Solution
Step 1: Understand the goal
The function should return the first string that is not empty or null from the input list.
Step 2: Analyze each option
function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item) { return $item }
}
return ''
} checks if the item is truthy (non-empty), then returns it immediately, else returns empty string if none found. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item -eq '') { return $item }
}
return $null
} returns empty strings, which is wrong. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if (-not $item) { return $item }
}
return 'empty'
} returns empty or null values, opposite of requirement. function Get-FirstNonEmpty {
param([string[]]$inputs)
return $inputs[0]
} always returns first item without checking if empty.
Final Answer:
if ($item) { return $item } -> Option D
Quick Check:
Return first truthy string using if ($item) [OK]
Hint: Return first truthy item with if ($item) inside loop [OK]
Common Mistakes:
Returning empty strings instead of non-empty
Returning null or empty by mistake
Not checking items before returning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.