nerdexam
Oracle

1Z0-811 · Question #38

Given: public static void main (String[] args) { boolean value1 = 10 + 5 >= 2 + 13; int value2 = 0; if (value1 == true) { value2 = 5 3 + 10 / 2; } else { value2 = 5 / 3 + 10 2; } System.out.println…

The correct answer is A. 20. Option A (20) is correct because value1 = (10 + 5 >= 2 + 13) evaluates to (15 >= 15), which is true, so the if branch executes. Applying standard Java operator precedence (multiplication and division before addition), 5 3 + 10 / 2 = 15 + 5 = 20. D (21) is wrong because it comes…

Data Types and Operators

Question

Given: public static void main (String[] args) { boolean value1 = 10 + 5 >= 2 + 13; int value2 = 0; if (value1 == true) { value2 = 5 * 3 + 10 / 2; } else { value2 = 5 / 3 + 10 * 2; } System.out.println (value2); } What is the result?

Options

  • A20
  • B32
  • CA compilation error occurs.
  • D21

How the community answered

(23 responses)
  • A
    74% (17)
  • B
    17% (4)
  • C
    4% (1)
  • D
    4% (1)

Explanation

Option A (20) is correct because value1 = (10 + 5 >= 2 + 13) evaluates to (15 >= 15), which is true, so the if branch executes. Applying standard Java operator precedence (multiplication and division before addition), 5 * 3 + 10 / 2 = 15 + 5 = 20.

D (21) is wrong because it comes from incorrectly believing value1 is false (perhaps confusing >= with >), which would send execution into the else branch: 5 / 3 + 10 * 2 = 1 + 20 = 21 (note the integer division).

B (32) is wrong - it results from misapplying operator precedence in the if branch, for example accidentally grouping 5 * (3 + 10) / 2 = 65 / 2 = 32.

C (compilation error) is wrong because the code is syntactically valid Java; value1 == true is redundant but legal.

Memory tip: When evaluating a boolean condition, first compute both sides completely, then apply the relational operator - >= means "greater than or equal to," so equal values still satisfy it. Then follow BODMAS/PEMDAS strictly inside the chosen branch.

Topics

#operator precedence#comparison operators#arithmetic evaluation#conditionals

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice