0
0
PowerShellscripting~10 mins

Ternary operator (PowerShell 7+) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the ternary operator to assign $result as 'Yes' if $value is greater than 10, otherwise 'No'.

PowerShell
$result = ($value -gt 10) [1] 'Yes' : 'No'
Drag options to blanks, or click blank then click option'
A?
B??
C:
D||
Attempts:
3 left
💡 Hint
Common Mistakes
Using '??' which is the null-coalescing operator, not ternary.
Using ':' alone without '?' to start the ternary.
Using '||' which is a logical OR operator.
2fill in blank
medium

Complete the code to assign $status as 'Adult' if $age is 18 or more, else 'Minor' using the ternary operator.

PowerShell
$status = ($age -ge 18) [1] 'Adult' : 'Minor'
Drag options to blanks, or click blank then click option'
A??
B:
C?
D&&
Attempts:
3 left
💡 Hint
Common Mistakes
Using '??' which is for null coalescing, not ternary.
Using ':' instead of '?'.
Using '&&' which is logical AND.
3fill in blank
hard

Fix the error in the ternary expression to correctly assign $output as 'Even' if $num is divisible by 2, else 'Odd'.

PowerShell
$output = ($num % 2 -eq 0) [1] 'Even' : 'Odd'
Drag options to blanks, or click blank then click option'
A&&
B:
C??
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '?' after the condition.
Using '??' which is null coalescing.
Using '&&' which is logical AND.
4fill in blank
hard

Complete the code to create a ternary expression that assigns $message as 'Success' if $code equals 200, else 'Error'.

PowerShell
$message = ($code -eq 200) [1] 'Success' : 'Error'
Drag options to blanks, or click blank then click option'
A?
B:
C??
D&&
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '?' and ':'.
Using '??' or '&&' instead of '?' or ':'.
5fill in blank
hard

Fill both blanks to create a nested ternary expression that assigns $result as 'Positive', 'Negative', or 'Zero' based on $num.

PowerShell
$result = ($num -gt 0) [1] 'Positive' : (($num -lt 0) [2] 'Negative' : 'Zero')
Drag options to blanks, or click blank then click option'
A?
B:
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing ':' and '?' in nested ternaries.
Missing parentheses around nested ternary.