Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard

Question 3

public class OperatorTest {

    public static void main(String[] args) {

        int x = 5;

        boolean result = x++ == 5 && ++x == 7 || x++ == 7;

        System.out.println("x = " + x);

        System.out.println("result = " + result);

    }

}


What will be the result of compiling and executing OperatorTest class?

  • A

    x = 7, result = true

  • B

    Compilation error

  • C

    x = 8, result = false

Reveal correct answer

Correct answer: A

Explanation

Explanation: The expression boolean result = x++ == 5 && ++x == 7 || x++ == 7; evaluates as follows:


  • x++ == 5 is true (x becomes 6)

  • ++x == 7 is true (x becomes 7)

  • The logical AND && combines the two true conditions, so the first part is true.

  • The OR || checks x++ == 7 but doesn't need to because the first part is already true.

  • Final values: x = 7, result = true.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need