📖 Scenario: You are creating a simple program that decides if a person is an adult or a minor based on their age.
🎯 Goal: Build a Java program that uses the ternary operator to check if a person is an adult (18 or older) or a minor (under 18) and prints the result.
📋 What You'll Learn
Create an integer variable age with the value 20
Create a string variable status that uses the ternary operator to set 'Adult' if age is 18 or more, otherwise 'Minor'
Print the status variable
💡 Why This Matters
🌍 Real World
Deciding between two options quickly based on a condition is common in apps, like showing messages based on user age.
💼 Career
Understanding the ternary operator helps write concise and readable code, a skill useful for any Java developer.
Progress0 / 4 steps
1
Create the age variable
Create an integer variable called age and set it to 20.
Java
Hint
Use int age = 20; inside the main method.
2
Create the status variable using the ternary operator
Create a string variable called status that uses the ternary operator to set it to "Adult" if age is 18 or more, otherwise "Minor".
Java
Hint
Use String status = age >= 18 ? "Adult" : "Minor"; to assign the value.
3
Print the status variable
Write a System.out.println statement to print the value of the status variable.
Java
Hint
Use System.out.println(status); to show the result.
4
Run the program to see the output
Run the program. It should print Adult because the age is 20.
Java
Hint
The program prints Adult because 20 is greater than or equal to 18.
Practice
(1/5)
1. What does the ternary operator condition ? valueIfTrue : valueIfFalse do in Java?
easy
A. It creates a loop that runs twice.
B. It chooses one of two values based on the condition's truth.
C. It declares a new variable.
D. It throws an error if the condition is false.
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 B
Quick 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
A. int result = (x > 0) ? (1 : -1);
B. int result = (x > 0) ? 1, -1;
C. int result = (x > 0) : 1 ? -1;
D. int result = (x > 0) ? 1 : -1;
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 D
Quick 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
A. 10
B. false
C. true
D. 5
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 A
Quick 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
A. Missing colon ':' in ternary operator
B. Incorrect variable declaration
C. Using comma instead of semicolon
D. No error, code runs fine
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.