Question 121 / 297:  What is the result of compiling and running the following code?
public class Tester {

int x = 12;

static void reset(Tester obj) {

obj = null;

}

public static void main(String[] args) {

Tester o1 = new Tester();

reset(o1);

System.out.print(o1.x);

}

}

A  0
B  12
C  Compilation error
D  NullPointerException is thrown
E  Another type of exception is thrown
<< First < Previous Next > Last >>
Explanation:

The variable is passed in Java by value not by reference. So it is not o1 that is assigned to null, rather the local variable obj in method reset, which was first assigned to the same value of o1 then to null.

In simple words:

calling method reset(o1) will make "obj", the local variable of method reset, refer to the same object "o1" refers to.

Inside method reset, "obj" will then be set to null (refers to nothing)

At this point only "obj" is affected not "o1", who still refers to the same object before calling reset(o1)

Exam Home Page
Ask
Hope Percle
Start Quiz
Jordon Humphreys
Start Quiz
Sarah Warren
Start Test
Briana Knowlton
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>