The condition is $age -ge 18, which means age greater or equal to 18. Since $age is 20, the condition is true.
Step 2: Apply ternary operator
Because the condition is true, the ternary operator returns 'Adult'.
Final Answer:
Adult -> Option C
Quick Check:
20 >= 18 ? 'Adult' : 'Minor' = Adult [OK]
Hint: Check condition result first, then pick true or false value [OK]
Common Mistakes:
Confusing true and false branches
Misreading the comparison operator
Expecting boolean output instead of string
4. Identify the error in this PowerShell ternary expression:
$score = 75 $grade = $score -gt 90 ? 'A' ? 'B'
medium
A. Missing colon ':' between true and false values.
B. Using wrong comparison operator '-gt'.
C. Variables must be declared with 'var' keyword.
D. Ternary operator cannot be used with strings.
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a question mark '?' after the condition, then the true-value, then a colon ':', then the false-value.
Step 2: Identify the error in the code
The code uses two question marks '?' but misses the colon ':'. This breaks the syntax.
Final Answer:
Missing colon ':' between true and false values. -> Option A
Quick Check:
Correct ternary = condition ? true : false [OK]
Hint: Remember ternary needs '?' then ':' in order [OK]
Common Mistakes:
Using '?' twice without ':'
Confusing comparison operators
Thinking variables need 'var' keyword
5. You want to assign a variable $status to 'Pass' if $score is 50 or more, 'Fail' otherwise. Which PowerShell ternary expression correctly does this?
hard
A. $status = if ($score -ge 50) { 'Pass' } else { 'Fail' }
B. $status = $score >= 50 ? 'Pass' : 'Fail'
C. $status = $score ? 'Pass' : 'Fail'
D. $status = $score -ge 50 ? 'Pass' : 'Fail'
Solution
Step 1: Understand the condition and syntax
The condition is $score -ge 50 (greater or equal 50). PowerShell uses '-ge' for numeric comparisons, and ternary syntax is condition ? true-value : false-value.
Step 2: Evaluate options
$status = $score -ge 50 ? 'Pass' : 'Fail' uses correct comparison operator '-ge' and correct ternary syntax. $status = $score >= 50 ? 'Pass' : 'Fail' uses '>=', which is invalid in PowerShell. $status = if ($score -ge 50) { 'Pass' } else { 'Fail' } uses if-else, not ternary. $status = $score ? 'Pass' : 'Fail' uses $score as condition directly, which is incorrect.