Practice mode Java

Given static void f(int a) { a = new int {9}; }, what does int b = {1}; f(b); System.out.println(b); print?

Correct answer D. 1

Explanation

Java passes every argument by value, and for an array that value is a copy of the reference. Reassigning the parameter a only changes the local copy, so b still refers to the original array and 1 is printed. Had the method executed a[0] = 9 instead, the shared array would change and 9 would appear.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.