nerdexam
Oracle

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.

Using Operators and Decision Constructs

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)
  • A
    89% (32)
  • B
    3% (1)
  • C
    6% (2)
  • D
    3% (1)

Why each option

This code evaluates boolean expressions using logical AND and OR operators, then prints the results of log1 and log2.

Alog1: falseCorrect

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`.

Blog1: true

The logical AND operator (&&) requires both operands to be true for the result to be true, but (1 > 5) is false.

Clog1: false

This choice is textually identical to the correct answer A, but A is specified as the correct option.

Dlog1: true

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

#boolean operators#logical AND#logical OR

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice