The ternary operator lets you choose between two values quickly based on a condition. It makes simple decisions easier and your code shorter.
Ternary operator in Java
Introduction
Syntax
Java
result = condition ? valueIfTrue : valueIfFalse;
The condition is a boolean expression that is either true or false.
If condition is true, valueIfTrue is used; otherwise, valueIfFalse is used.
Examples
a or b to max.Java
int max = (a > b) ? a : b;score is 50 or more, otherwise "Fail".Java
String result = (score >= 50) ? "Pass" : "Fail";
Java
System.out.println(isSunny ? "Take sunglasses" : "Take umbrella");
Sample Program
This program checks if someone is old enough to vote. It uses the ternary operator to set "Yes" or "No" based on the age.
Java
public class TernaryExample { public static void main(String[] args) { int age = 18; String canVote = (age >= 18) ? "Yes" : "No"; System.out.println("Can vote? " + canVote); } }
Important Notes
The ternary operator is best for simple decisions. For complex logic, use if-else for clarity.
Both valueIfTrue and valueIfFalse should be of compatible types.
Summary
The ternary operator is a short way to choose between two values based on a condition.
It uses the syntax: condition ? valueIfTrue : valueIfFalse.
Use it to make your code shorter and easier to read for simple decisions.
Practice
1. What does the ternary operator
condition ? valueIfTrue : valueIfFalse do in Java?easy
Solution
Step 1: Understand the ternary operator syntax
The ternary operator uses a condition followed by a question mark, then two values separated by a colon.Step 2: Determine its purpose
It returns the first value if the condition is true, otherwise the second value.Final Answer:
It chooses one of two values based on the condition's truth. -> Option BQuick Check:
condition ? trueValue : falseValue = chooses value [OK]
Hint: Think: condition true? pick first : else pick second [OK]
Common Mistakes:
- Confusing ternary with loops
- Thinking it declares variables
- Assuming it throws errors automatically
2. Which of the following is the correct syntax for a ternary operator in Java?
easy
Solution
Step 1: Review ternary operator syntax
The correct syntax is: condition ? valueIfTrue : valueIfFalse;Step 2: Check each option
int result = (x > 0) ? 1 : -1; matches the correct syntax exactly. Others have wrong punctuation or order.Final Answer:
int result = (x > 0) ? 1 : -1; -> Option DQuick Check:
condition ? trueValue : falseValue syntax [OK]
Hint: Remember: question mark then colon separate values [OK]
Common Mistakes:
- Using comma instead of colon
- Swapping question mark and colon
- Adding extra parentheses incorrectly
3. What is the output of this Java code?
int a = 5;
int b = 10;
int max = (a > b) ? a : b;
System.out.println(max);
medium
Solution
Step 1: Evaluate the condition (a > b)
Since a=5 and b=10, 5 > 10 is false.Step 2: Apply ternary operator
Because condition is false, max gets value b, which is 10.Final Answer:
10 -> Option AQuick Check:
(5 > 10) ? 5 : 10 = 10 [OK]
Hint: Check condition result true or false to pick value [OK]
Common Mistakes:
- Choosing the first value always
- Confusing variable values
- Expecting boolean output
4. Identify the error in this code snippet:
int x = 10;
int y = 20;
int min = x > y ? x, y;
System.out.println(min);
medium
Solution
Step 1: Check ternary operator syntax
The ternary operator requires a colon ':' between true and false values.Step 2: Identify the error in code
The code uses a comma ',' instead of a colon ':', causing a syntax error.Final Answer:
Missing colon ':' in ternary operator -> Option AQuick Check:
condition ? valueIfTrue : valueIfFalse needs ':' [OK]
Hint: Ternary needs '?' then ':'; commas cause errors [OK]
Common Mistakes:
- Using comma instead of colon
- Forgetting colon entirely
- Misplacing semicolons inside ternary
5. Given this code, what is the value of
result? int score = 75;
String result = (score >= 90) ? "A" : (score >= 80) ? "B" : (score >= 70) ? "C" : "F";
System.out.println(result);
hard
Solution
Step 1: Evaluate nested ternary conditions in order
score=75. Check if score >= 90? No. Then check if score >= 80? No. Then check if score >= 70? Yes.Step 2: Assign value based on first true condition
Since score >= 70 is true, result is assigned "C".Final Answer:
"C" -> Option CQuick Check:
Nested ternary picks first true condition value [OK]
Hint: Read nested ternary top to bottom, pick first true [OK]
Common Mistakes:
- Stopping at first false condition
- Ignoring nested ternary order
- Confusing string quotes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
