KnowledgeBoat Logo
|

Computer Applications

Consider the following code snippet

if (c > d)
    x = c;
else
    x = d;

Choose the correct option if the code mentioned above is rewritten using the ternary operator.

  1. x = (c > d) ? c : d;
  2. x = (c > d) ? d : c;
  3. x = (c > d) ? c : c;
  4. x = (c > d) ? d : d;

Java Conditional Stmts

26 Likes

Answer

x = (c > d) ? c : d;

Reason — Correct syntax of ternary operator is:

variable = (test expression) ? exp 1 : exp 2

If the test expression is true, exp 1 is assigned to variable, else exp 2 is assigned to the variable.

Answered By

10 Likes


Related Questions