Challenge - 5 Problems
PowerShell Ternary 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 ternary expression?
Consider the following PowerShell code snippet using the ternary operator. What will it output?
PowerShell
$value = 10; $result = ($value -gt 5) ? 'Greater' : 'Smaller'; Write-Output $result
Attempts:
2 left
💡 Hint
Check if the condition $value -gt 5 is true or false.
✗ Incorrect
The condition $value -gt 5 checks if 10 is greater than 5, which is true, so the ternary operator returns 'Greater'.
📝 Syntax
intermediate2:00remaining
Which option correctly uses the ternary operator to assign a value?
Select the PowerShell code snippet that correctly uses the ternary operator to assign 'Even' or 'Odd' to $parity based on $num.
Attempts:
2 left
💡 Hint
Remember PowerShell uses '-eq' for equality and the ternary operator requires '?' and ':'
✗ Incorrect
Option D uses the correct equality operator '-eq' and the proper ternary syntax '? :'. Option D uses '==' which is invalid in PowerShell. Option D misses the ':' separator. Option D uses an if-else statement, not a ternary operator.
🔧 Debug
advanced2:00remaining
Identify the error in this ternary operator usage
What error will this PowerShell code produce?
PowerShell
$x = 5; $y = $x -gt 3 ? 'Yes' : 'No'; Write-Output $y
Attempts:
2 left
💡 Hint
Check the syntax for the ternary operator in PowerShell 7+.
✗ Incorrect
PowerShell requires the condition to be enclosed in parentheses when using the ternary operator. Without parentheses, the '?' is unexpected, causing a syntax error.
🚀 Application
advanced2:00remaining
Using ternary operator to simplify a script
You want to assign 'Adult' if $age is 18 or more, otherwise 'Minor'. Which code snippet correctly uses the ternary operator to do this?
Attempts:
2 left
💡 Hint
PowerShell uses '-ge' for greater or equal comparison and ternary operator syntax is '? :'.
✗ Incorrect
Option C correctly uses '-ge' and the ternary operator syntax. Option C uses if-else, not ternary. Option C uses '>=' which is invalid in PowerShell. Option C reverses the true and false values.
🧠 Conceptual
expert3:00remaining
What is the value of $output after running this code?
Given the code below, what is the value of $output?
PowerShell
$a = 4; $b = 7; $output = ($a -eq 4) ? (($b -gt 5) ? 'X' : 'Y') : 'Z'; Write-Output $output
Attempts:
2 left
💡 Hint
Evaluate the nested ternary operators step by step.
✗ Incorrect
First, $a -eq 4 is true, so evaluate the nested ternary: $b -gt 5 is true (7 > 5), so the inner ternary returns 'X'.