What is the ternary conditional operator in Swift?
It is a shorthand way to write an if-else statement using the syntax: condition ? valueIfTrue : valueIfFalse.
Click to reveal answer
beginner
How does the ternary operator improve code readability?
It makes simple conditional assignments or expressions shorter and easier to read by reducing multiple lines into one line.
Click to reveal answer
beginner
Write a Swift example using the ternary operator to assign 'adult' or 'minor' based on age.
let status = age >= 18 ? "adult" : "minor"
Click to reveal answer
intermediate
Can the ternary operator be nested in Swift? If yes, what should you be careful about?
Yes, it can be nested but it can make code hard to read and understand. Use parentheses and keep nesting minimal.
Click to reveal answer
intermediate
What types of expressions can be used with the ternary conditional operator in Swift?
Any expressions that return a value can be used, as long as the true and false parts return the same type or compatible types.
Click to reveal answer
What is the correct syntax of the ternary conditional operator in Swift?
Acondition ? valueIfTrue : valueIfFalse
Bif condition then valueIfTrue else valueIfFalse
Ccondition : valueIfTrue ? valueIfFalse
Dcondition ? valueIfFalse : valueIfTrue
✗ Incorrect
The ternary operator uses the syntax: condition ? valueIfTrue : valueIfFalse.
What will this Swift code print? let age = 20; print(age >= 18 ? "Adult" : "Minor")
AError
BMinor
C20
DAdult
✗ Incorrect
Since age is 20 which is >= 18, the condition is true, so it prints "Adult".
Can the ternary operator replace all if-else statements in Swift?
AOnly for loops
BYes, always
CNo, only simple conditional expressions
DOnly for functions
✗ Incorrect
The ternary operator is best for simple conditions; complex logic is clearer with if-else.
What happens if the true and false expressions in a ternary operator return different types?
ACompilation error
BIt will run but give wrong results
CSwift will automatically convert types
DIt will ignore the false expression
✗ Incorrect
Swift requires both expressions to have compatible types; otherwise, it causes a compile error.
Which of these is a good use of the ternary operator?
AReplacing complex nested if-else blocks
BAssigning a value based on a simple condition
CWriting loops
DDeclaring functions
✗ Incorrect
Ternary operator is ideal for simple conditional assignments, not complex logic.
Explain how the ternary conditional operator works in Swift and give a simple example.
Think of it as a short if-else that picks one of two values.
You got /5 concepts.
Describe when you should use the ternary operator and when to avoid it in Swift programming.
Consider how easy it is to read your code.
You got /4 concepts.
Practice
(1/5)
1. What does the ternary conditional operator do in Swift? condition ? valueIfTrue : valueIfFalse
easy
A. Declares a constant value
B. Repeats a block of code multiple times
C. Defines a new function
D. Chooses one of two values based on a condition
Solution
Step 1: Understand the operator structure
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
Step 2: Identify its purpose
It selects the first value if the condition is true, otherwise the second value.
Final Answer:
Chooses one of two values based on a condition -> Option D
Quick Check:
Ternary operator = chooses between two values [OK]
Hint: Remember: condition ? trueValue : falseValue picks one [OK]
Common Mistakes:
Confusing ternary with loops
Thinking it declares variables
Mixing it with function syntax
2. Which of the following is the correct syntax for the ternary conditional operator in Swift?
easy
A. condition ? valueIfTrue : valueIfFalse
B. condition : valueIfTrue ? valueIfFalse
C. condition ? (valueIfTrue, valueIfFalse)
D. condition ? valueIfTrue; valueIfFalse
Solution
Step 1: Recall the ternary syntax
The correct syntax places the condition first, then a question mark, then the true value, a colon, and the false value.
Step 2: Compare options
Only condition ? valueIfTrue : valueIfFalse matches this exact pattern.
Final Answer:
condition ? valueIfTrue : valueIfFalse -> Option A
Quick Check:
Syntax is condition ? true : false [OK]
Hint: Question mark before colon is key in ternary syntax [OK]
Common Mistakes:
Swapping question mark and colon
Using commas instead of colon
Adding semicolons inside ternary
3. What is the output of this Swift code?
let age = 20
let canVote = age >= 18 ? "Yes" : "No"
print(canVote)
medium
A. No
B. Yes
C. true
D. false
Solution
Step 1: Evaluate the condition
age is 20, which is greater than or equal to 18, so the condition is true.
Step 2: Apply ternary operator
Since the condition is true, canVote is assigned "Yes".
Final Answer:
Yes -> Option B
Quick Check:
age >= 18 is true, so output is "Yes" [OK]
Hint: Check condition result first, then pick true or false value [OK]
Common Mistakes:
Confusing true and false branches
Assuming boolean output instead of string
Ignoring the condition value
4. Identify the error in this Swift code using the ternary operator:
let score = 85
let grade = score > 90 ? "A" : "B" else "C"
medium
A. Using double quotes instead of single quotes
B. Missing parentheses around condition
C. Extra 'else' keyword after ternary operator
D. Incorrect variable declaration
Solution
Step 1: Analyze ternary operator usage
The ternary operator already includes both true and false values separated by a colon; adding 'else' is invalid syntax.
Step 2: Identify the syntax error
The 'else' keyword after the ternary expression causes a syntax error.
Final Answer:
Extra 'else' keyword after ternary operator -> Option C
Quick Check:
No 'else' after ternary operator [OK]
Hint: Ternary uses colon, never 'else' keyword [OK]
Common Mistakes:
Adding 'else' after ternary
Confusing ternary with if-else blocks
Misplacing colons and question marks
5. Given a dictionary of student scores, use a ternary operator in Swift to create a new dictionary that marks each student as "Pass" if their score is 50 or more, otherwise "Fail". What is the correct way to write this using dictionary comprehension?
let scores = ["Anna": 75, "Ben": 45, "Cara": 50]
let results = scores.mapValues { score in
// Fill in here
}
hard
A. score >= 50 ? "Pass" : "Fail"
B. if score >= 50 { "Pass" } else { "Fail" }
C. score > 50 ? "Pass" : "Fail"
D. score >= 50 ? "Fail" : "Pass"
Solution
Step 1: Understand the condition for passing
Students pass if their score is 50 or more, so condition is score >= 50.
Step 2: Apply ternary operator correctly
Use ternary to return "Pass" if true, else "Fail".
Final Answer:
score >= 50 ? "Pass" : "Fail" -> Option A
Quick Check:
Use condition ? "Pass" : "Fail" for mapping [OK]
Hint: Use mapValues with ternary: condition ? trueVal : falseVal [OK]
Common Mistakes:
Using if-else instead of ternary in mapValues
Wrong comparison operator (>)
Swapping Pass and Fail values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.