Recall & Review
beginner
What is the ternary operator in Java?
The ternary operator is a shortcut for an if-else statement. It uses the syntax: condition ? valueIfTrue : valueIfFalse;
Click to reveal answer
beginner
How does the ternary operator improve code?
It makes code shorter and easier to read when choosing between two values based on a condition.
Click to reveal answer
beginner
Example: What does this code print?<br>
int a = 5; int b = 10; System.out.println(a > b ? a : b);
It prints 10 because the condition (a > b) is false, so it chooses the value after the colon, which is b (10).
Click to reveal answer
intermediate
Can the ternary operator be nested in Java?
Yes, you can nest ternary operators inside each other, but it can make code harder to read.
Click to reveal answer
intermediate
What types of values can the ternary operator return?
It can return any type as long as both possible values are of compatible types (e.g., both int, or both String).
Click to reveal answer
What is the correct syntax of the ternary operator in Java?
✗ Incorrect
The ternary operator syntax is condition ? valueIfTrue : valueIfFalse.
What will this print?<br>
int x = 3; System.out.println(x > 5 ? "Yes" : "No");
✗ Incorrect
Since 3 > 5 is false, it prints the value after the colon: No.
Can the ternary operator replace all if-else statements?
✗ Incorrect
Ternary operator is best for simple if-else that choose between two values.
What happens if you nest ternary operators too much?
✗ Incorrect
Nesting ternary operators can make code confusing and hard to maintain.
Which of these is a valid use of the ternary operator?
✗ Incorrect
Option A uses correct ternary syntax to assign max.
Explain how the ternary operator works in Java and give a simple example.
Think of it as a short if-else that picks one of two values.
You got /3 concepts.
What are the advantages and disadvantages of using the ternary operator?
Consider when it helps and when it might confuse.
You got /3 concepts.