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…
Question
Options
- A20
- B32
- CA compilation error occurs.
- D21
How the community answered
(23 responses)- A74% (17)
- B17% (4)
- C4% (1)
- D4% (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
Community Discussion
No community discussion yet for this question.