Recall & Review
beginner Click to reveal answer
What is the ternary operator in C?
The ternary operator is a shorthand way to write an if-else statement. It uses the syntax: condition ? expression_if_true : expression_if_false;
beginner Click to reveal answer
How does the ternary operator work in C?
It evaluates the condition first. If the condition is true, it returns the first expression; if false, it returns the second expression.
beginner Click to reveal answer
Rewrite this if-else using the ternary operator:
if (a > b) {
max = a;
} else {
max = b;
}Using ternary operator:
max = (a > b) ? a : b;
intermediate Click to reveal answer
Can the ternary operator be nested in C?
Yes, you can nest ternary operators inside each other, but it can make code harder to read. Example:
result = (a > b) ? a : ((b > c) ? b : c);
beginner Click to reveal answer
What is a common use case for the ternary operator?
It is often used for simple conditional assignments or returning values in a concise way instead of writing full if-else blocks.
What does the ternary operator syntax look like in C?
✗ Incorrect
The ternary operator uses the syntax: condition ? expression_if_true : expression_if_false.
What value does the ternary operator return if the condition is false?
✗ Incorrect
If the condition is false, the ternary operator returns the second expression after the colon.
Which of these is a correct use of the ternary operator?
✗ Incorrect
Option B correctly uses the ternary operator syntax.
Is it recommended to use nested ternary operators?
✗ Incorrect
Nested ternary operators are allowed but can make code harder to read.
What is a benefit of using the ternary operator?
✗ Incorrect
The ternary operator simplifies writing simple conditional assignments in a concise way.
Explain how the ternary operator works and write a simple example in C.
Think of it as a short if-else that returns a value.
You got /4 concepts.
Describe when you might prefer using the ternary operator instead of a full if-else statement.
Use it when you want to assign a value quickly based on a condition.
You got /3 concepts.
