Bird
Raised Fist0
PowerShellscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the ternary operator condition ? true-value : false-value do in PowerShell 7+?
easy
A. It comments out a line of code.
B. It chooses one of two values based on a condition.
C. It defines a function with two parameters.
D. It creates a loop that runs twice.

Solution

  1. Step 1: Understand the ternary operator structure

    The ternary operator uses a condition followed by a question mark, then a value if true, a colon, and a value if false.
  2. Step 2: Identify its purpose

    It evaluates the condition and returns the true-value if the condition is true, otherwise the false-value.
  3. Final Answer:

    It chooses one of two values based on a condition. -> Option B
  4. Quick Check:

    Ternary operator = choose value based on condition [OK]
Hint: Think: condition ? yes : no picks one value fast [OK]
Common Mistakes:
  • Confusing ternary with loops
  • Thinking it defines functions
  • Assuming it comments code
2. Which of the following is the correct syntax for the ternary operator in PowerShell 7+?
easy
A. condition ? true-value : false-value
B. if (condition) { true-value } else { false-value }
C. condition ? true-value, false-value
D. condition : true-value ? false-value

Solution

  1. Step 1: Recall the ternary operator syntax

    The ternary operator uses the format: condition ? true-value : false-value.
  2. Step 2: Compare options

    'if (condition) { true-value } else { false-value }' is an if-else statement, not ternary. 'condition ? true-value, false-value' uses comma instead of colon. 'condition : true-value ? false-value' has question mark and colon swapped. Only 'condition ? true-value : false-value' matches exactly.
  3. Final Answer:

    condition ? true-value : false-value -> Option A
  4. Quick Check:

    Correct ternary syntax = condition ? true : false [OK]
Hint: Look for question mark then colon in correct order [OK]
Common Mistakes:
  • Using commas instead of colon
  • Swapping question mark and colon
  • Using if-else instead of ternary
3. What is the output of this PowerShell code?
$age = 20
$result = $age -ge 18 ? 'Adult' : 'Minor'
Write-Output $result
medium
A. True
B. Minor
C. Adult
D. False

Solution

  1. Step 1: Evaluate the condition

    The condition is $age -ge 18, which means age greater or equal to 18. Since $age is 20, the condition is true.
  2. Step 2: Apply ternary operator

    Because the condition is true, the ternary operator returns 'Adult'.
  3. Final Answer:

    Adult -> Option C
  4. 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

  1. 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.
  2. Step 2: Identify the error in the code

    The code uses two question marks '?' but misses the colon ':'. This breaks the syntax.
  3. Final Answer:

    Missing colon ':' between true and false values. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    $status = $score -ge 50 ? 'Pass' : 'Fail' -> Option D
  4. Quick Check:

    PowerShell ternary with '-ge' = $status = $score -ge 50 ? 'Pass' : 'Fail' [OK]
Hint: Use '-ge' for numeric compare and '?' then ':' for ternary [OK]
Common Mistakes:
  • Using '>=', which is invalid in PowerShell
  • Using if-else instead of ternary
  • Using variable alone as condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard