Consider the following code segment. Assume the statements after the method appear in another method in the same class. What is printed as a result of executing the code segment?
public static int addOne(int n) {
return n + 1;
}
int points = 8;
addOne(points);
System.out.println(points);
890points, which still prints as 8.
Explanation
For a primitive argument, changing the method's copied parameter does not change the caller's variable. The call also stands alone as a statement, so the value it returns is discarded and the variable keeps the value it already had.
- B is not correct: the method does return
9, but that returned value is used as a standalone statement and discarded, sopointsis unchanged. - C is not correct: nothing in the code segment assigns
0topoints, which is given the value8before the call and never reassigned. - D is not correct: the call matches the parameter list of
addOne, and a non-voidcall may legally be used as a standalone statement.