What is printed by the following code snippet?
int f = 1 + 2 * 5>=2 ? 4 : 2; int g = 3 < 3 ? 1 : 5>=5 ? 9 : 7; System.out.print(f+g+"");
B.
The code compiles without issue, so Option E is incorrect.
+ and * have a higher precedence than the ternary ? : operator.
In the first expression, 1 + 2 * 5 is evaluated first, resulting in a reduction to 11>=2 ? 4 : 2, and then f being assigned a value of 4.
In the second expression, the first ternary expression evaluates to false resulting in a reduction to the second right-hand expression 5>=5 ? 9 : 7, which then assigns a value of 9 to g.
In the print()
statement, the first + operator is an addition operator, since the operands are numbers, resulting in the value of 4 + 9, 13.
The second + operator is a concatenation since one of the two operands is a String.
The result 13 is printed, making Option B the correct answer.