19.5k views
3 votes
Which of the following shows the correct syntax for the conditional operator?

a. testExpression ? falseResult : trueResult;
b. testExpression : trueResult : falseResult;
c. testExpression ? trueResult : falseResult;
d. testExpression ? trueResult ? falseResult;

User Axon
by
8.3k points

1 Answer

3 votes

The correct syntax for the conditional operator is testExpression ? trueResult : falseResult. It is used to return a value based on a condition.

The correct syntax for the conditional operator is testExpression ? trueResult : falseResult;. This means that if the testExpression evaluates to true, the trueResult will be returned, otherwise the falseResult will be returned.

For example, consider the following code:

int x = 10;String result = (x > 5) ? "x is greater than 5" : "x is less than or equal to 5";

In this code, if the value of x is greater than 5, the result will be "x is greater than 5", otherwise it will be "x is less than or equal to 5".