0
0
PowerShellscripting~20 mins

Ternary operator (PowerShell 7+) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PowerShell Ternary 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 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
AFalse
BGreater
CSmaller
DTrue
Attempts:
2 left
💡 Hint
Check if the condition $value -gt 5 is true or false.
📝 Syntax
intermediate
2: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.
A$parity = if ($num % 2 -eq 0) { 'Even' } else { 'Odd' }
B$parity = ($num % 2 == 0) ? 'Even' : 'Odd'
C$parity = ($num % 2 -eq 0) ? 'Even' else 'Odd'
D$parity = ($num % 2 -eq 0) ? 'Even' : 'Odd'
Attempts:
2 left
💡 Hint
Remember PowerShell uses '-eq' for equality and the ternary operator requires '?' and ':'
🔧 Debug
advanced
2: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
ARuntime error: Unexpected token '?'
BNo error, outputs 'Yes'
CSyntaxError: Missing parentheses around condition
DOutput is 'No'
Attempts:
2 left
💡 Hint
Check the syntax for the ternary operator in PowerShell 7+.
🚀 Application
advanced
2: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?
A$status = if ($age -ge 18) { 'Adult' } else { 'Minor' }
B'roniM' : 'tludA' ? )81 eg- ega$( = sutats$
C$status = ($age -ge 18) ? 'Adult' : 'Minor'
D$status = ($age -ge 18) ? 'Minor' : 'Adult'
Attempts:
2 left
💡 Hint
PowerShell uses '-ge' for greater or equal comparison and ternary operator syntax is '? :'.
🧠 Conceptual
expert
3: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
AX
BY
CZ
D7
Attempts:
2 left
💡 Hint
Evaluate the nested ternary operators step by step.