Courseiva
Controlling Program FlowmediumMultiple ChoiceObjective-mapped

1Z0-829 Ternary Operator Precedence Practice Question

Given: int a=5, b=10; String result = (a > b) ? "greater" : (a < b) ? "less" : "equal"; What is result?

⚠ Common exam trap

Candidates may think the expression causes a compilation error due to misinterpretation of grouping, but Java's right-associativity ensures it works correctly. The trap is to recognize that nested ternaries are valid and the precedence yields the expected result.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

"less"

The ternary operator is right-associative, so the expression is parsed as `a > b ? "greater" : (a < b ? "less" : "equal")`. Since `a=5` and `b=10`, `a > b` is false, so the second operand of the outer ternary is evaluated: `a < b` is true, so the inner ternary returns `"less"`. The code compiles and runs without error, producing `"less"`.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • "equal"

    Why it's wrong here

    This is incorrect because 'equal' would only be returned if a == b, but a=5 and b=10 are not equal.

  • "greater"

    Why it's wrong here

    This is incorrect because 'greater' would require a > b, but 5 > 10 is false.

  • Compilation error

    Why it's wrong here

    This is incorrect. The code compiles successfully; there is no type mismatch because the ternary operator is right-associative and the second operand of the outer ternary is the inner ternary, which returns a String.

  • "less"

    Why this is correct

    This is correct. The expression evaluates to 'less' because a=5 is less than b=10.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.