1Z0-803 · Question #249
Given: public class TestOperator { public static void main(String[] args) { int result = 30 -12 / (2*5)+1; System.out.print("Result = " + result); } } What is the result?
The correct answer is E. Result = 30. This question tests knowledge of operator precedence in Java, particularly integer division and the order of arithmetic operations. The calculation follows standard mathematical precedence: parentheses, division, then subtraction and addition from left to right.
Question
Given:
public class TestOperator { public static void main(String[] args) { int result = 30 -12 / (2*5)+1; System.out.print("Result = " + result); } } What is the result?
Options
- AResult = 2
- BResult = 3
- CResult = 28
- DResult = 29
- EResult = 30
How the community answered
(32 responses)- A3% (1)
- D3% (1)
- E94% (30)
Why each option
This question tests knowledge of operator precedence in Java, particularly integer division and the order of arithmetic operations. The calculation follows standard mathematical precedence: parentheses, division, then subtraction and addition from left to right.
This suggests an incorrect order of operations or a miscalculation in division.
This suggests an incorrect order of operations or a miscalculation in division.
This implies a different or incorrect application of operator precedence, likely misinterpreting integer division or the order of addition/subtraction.
This implies the final addition of `+1` was either missed or applied incorrectly after the division and subtraction.
Following Java's operator precedence, `(2*5)` is evaluated first to `10`. Then, `12 / 10` performs integer division, resulting in `1`. Finally, `30 - 1 + 1` is evaluated from left to right as `29 + 1`, which equals `30`.
Concept tested: Java operator precedence and integer division
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Topics
Community Discussion
No community discussion yet for this question.