nerdexam
Oracle

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.

Using Operators and Decision Constructs

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)
  • A
    3% (1)
  • D
    3% (1)
  • E
    94% (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.

AResult = 2

This suggests an incorrect order of operations or a miscalculation in division.

BResult = 3

This suggests an incorrect order of operations or a miscalculation in division.

CResult = 28

This implies a different or incorrect application of operator precedence, likely misinterpreting integer division or the order of addition/subtraction.

DResult = 29

This implies the final addition of `+1` was either missed or applied incorrectly after the division and subtraction.

EResult = 30Correct

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

#operators#operator precedence#arithmetic operations#integer division

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice