Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The ternary operator in PowerShell 7+ uses the syntax: condition ? true_value : false_value. Here, the '?' is the correct operator to start the ternary expression.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '??' which is for null coalescing, not ternary.
Using ':' instead of '?'.
Using '&&' which is logical AND.
✗ Incorrect
The ternary operator uses '?' after the condition to choose between two values. Here, '? ' is needed after the condition.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '?' after the condition.
Using '??' which is null coalescing.
Using '&&' which is logical AND.
✗ Incorrect
The ternary operator requires '?' after the condition to separate it from the true value. ':' separates true and false values.
4fill in blank
hardComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '?' and ':'.
Using '??' or '&&' instead of '?' or ':'.
✗ Incorrect
The ternary operator syntax is: condition ? true_value : false_value. '?' starts the ternary, ':' separates true and false parts.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing ':' and '?' in nested ternaries.
Missing parentheses around nested ternary.
✗ Incorrect
Nested ternary uses '?' and ':' repeatedly. First '?' after first condition, ':' before nested ternary, then '?' for nested condition.