Bird
Raised Fist0
Javaprogramming~20 mins

Ternary operator in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested ternary operator
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        int x = 5;
        String result = x > 0 ? (x < 10 ? "Small" : "Medium") : "Negative";
        System.out.println(result);
    }
}
ACompilation error
BMedium
CNegative
DSmall
Attempts:
2 left
💡 Hint
Check the conditions in the nested ternary carefully.
❓ Predict Output
intermediate
2:00remaining
Ternary operator with boolean expressions
What will be printed by this Java program?
Java
public class Main {
    public static void main(String[] args) {
        boolean a = true;
        boolean b = false;
        String output = a ? (b ? "Both true" : "Only a true") : "a is false";
        System.out.println(output);
    }
}
AOnly a true
BBoth true
Ca is false
DCompilation error
Attempts:
2 left
💡 Hint
Evaluate the conditions step by step.
🔧 Debug
advanced
2:00remaining
Identify the error in ternary usage
What error does this Java code produce?
Java
public class Main {
    public static void main(String[] args) {
        int x = 10;
        String result = x > 5 ? "Greater" : 5;
        System.out.println(result);
    }
}
ANo error, prints "Greater"
BType mismatch: cannot convert from int to String
CNo error, prints "5"
DSyntax error: missing colon
Attempts:
2 left
💡 Hint
Check the types of both expressions in the ternary operator.
🧠 Conceptual
advanced
2:00remaining
Ternary operator evaluation order
Consider this Java code snippet:
int a = 3;
int b = 4;
int max = a > b ? a : b;
Which statement is true about how the ternary operator works here?
ABoth a and b are evaluated before the ternary operator returns a value
BThe ternary operator always evaluates both expressions regardless of the condition
COnly the expression corresponding to the true condition is evaluated
DThe ternary operator evaluates neither expression and returns the condition's boolean value
Attempts:
2 left
💡 Hint
Think about short-circuit behavior in conditional expressions.
❓ Predict Output
expert
2:00remaining
Complex ternary operator with method calls
What is the output of this Java program?
Java
public class Main {
    static String check(int n) {
        return n % 2 == 0 ? "Even" : "Odd";
    }
    public static void main(String[] args) {
        int x = 7;
        String result = x > 5 ? check(x) : "Too small";
        System.out.println(result);
    }
}
AOdd
BToo small
CCompilation error
DEven
Attempts:
2 left
💡 Hint
Evaluate the condition and then the method call.

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

  1. 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.
  2. Step 2: Determine its purpose

    It returns the first value if the condition is true, otherwise the second value.
  3. Final Answer:

    It chooses one of two values based on the condition's truth. -> Option B
  4. 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

  1. Step 1: Review ternary operator syntax

    The correct syntax is: condition ? valueIfTrue : valueIfFalse;
  2. Step 2: Check each option

    int result = (x > 0) ? 1 : -1; matches the correct syntax exactly. Others have wrong punctuation or order.
  3. Final Answer:

    int result = (x > 0) ? 1 : -1; -> Option D
  4. 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

  1. Step 1: Evaluate the condition (a > b)

    Since a=5 and b=10, 5 > 10 is false.
  2. Step 2: Apply ternary operator

    Because condition is false, max gets value b, which is 10.
  3. Final Answer:

    10 -> Option A
  4. 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

  1. Step 1: Check ternary operator syntax

    The ternary operator requires a colon ':' between true and false values.
  2. Step 2: Identify the error in code

    The code uses a comma ',' instead of a colon ':', causing a syntax error.
  3. Final Answer:

    Missing colon ':' in ternary operator -> Option A
  4. Quick 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
A. "A"
B. "B"
C. "C"
D. "F"

Solution

  1. 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.
  2. Step 2: Assign value based on first true condition

    Since score >= 70 is true, result is assigned "C".
  3. Final Answer:

    "C" -> Option C
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard