What is the value of y after executing the following code? ```java int y = 10 + 12; ```
The value of y is 22 because Java's operator precedence rules dictate that multiplication operations are evaluated before addition. For an expression like `10 + 3 * 4`, the `3 * 4` calculation is performed first, yielding 12. Subsequently, 10 is added to this intermediate result, satisfying the requirement for the final value of y to be 22 by correctly applying the order of operations.
Why this answer
The code snippet `int y = 10 + 12;` performs integer addition, resulting in y = 22. Java's primitive arithmetic follows standard rules, so adding 10 and 12 yields 22 without any overflow or conversion issues.
Exam trap
Oracle often tests the candidate's attention to basic arithmetic with integer literals, where a simple misreading of the operands or operator leads to selecting a plausible but incorrect sum.
How to eliminate wrong answers
Option B is wrong because 20 would result from an incorrect operation like subtracting 2 instead of adding, or misreading the operands. Option C is wrong because 21 would come from a miscalculation such as 10 + 11 or a off-by-one error. Option D is wrong because 23 would require an extra increment or addition of 1 beyond the correct sum.