1Z0-803 · Question #142
Given the code fragment: boolean log1 = (1 < 9) && (1 > 5); boolean log2 = (3 == 4) || (3 == 3); System.out.println("log1:" + log1 + "\nlog2:" + log2); What is the result?
The correct answer is A. log1: false. This code evaluates boolean expressions using logical AND and OR operators, then prints the results of log1 and log2.
Question
Given the code fragment:
boolean log1 = (1 < 9) && (1 > 5); boolean log2 = (3 == 4) || (3 == 3); System.out.println("log1:" + log1 + "\nlog2:" + log2); What is the result?
Options
- Alog1: false
- Blog1: true
- Clog1: false
- Dlog1: true
How the community answered
(36 responses)- A89% (32)
- B3% (1)
- C6% (2)
- D3% (1)
Why each option
This code evaluates boolean expressions using logical AND and OR operators, then prints the results of log1 and log2.
The expression (1 < 9) evaluates to true, and (1 > 5) evaluates to false; applying the logical AND operator (&&) to true && false results in false for `log1`.
The logical AND operator (&&) requires both operands to be true for the result to be true, but (1 > 5) is false.
This choice is textually identical to the correct answer A, but A is specified as the correct option.
The logical AND operator (&&) requires both operands to be true for the result to be true, but (1 > 5) is false.
Concept tested: Java boolean logical operators
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
Topics
Community Discussion
No community discussion yet for this question.