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

Question 18

public class NumberTest {

    public static void main(String[] args) {

        int a = 3;

        modifyValues(++a, a++);

        System.out.println(a);

    }


    private static void modifyValues(int x, int y) {

        x++;

        y--;

    }

}

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

  • A

    4

  • B

    3

  • C

    6

  • D

    5

Reveal correct answer

Correct answer: D

Explanation

The modifyValues method works on copies of the arguments, so changes done to x and y do not affect the original variables. The sequence is as follows:

  • a = 3

  • modifyValues(++a, a++) results in modifyValues(4, 4) and a = 5.

  • After the method call, a remains 5.

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