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 = 3modifyValues(++a, a++)results inmodifyValues(4, 4)anda = 5.After the method call,
aremains 5.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
