Given:
and
What is the result?
Given:
and
What is the result?
In the given code, the method checkPerson(Person p) assigns a new Person object with the name 'Mary' only if the passed parameter p is null. In the main method, when checkPerson(p) is first called, p is null, so it gets assigned a new Person object with the name 'Mary'. The println statement then prints 'Mary'. Next, p1 is assigned a new Person object with the name 'Joe'. When checkPerson(p) is called again with the argument p, which now references 'Mary', the if condition fails (since p is not null), and it returns the existing p without changing it. Therefore, the second println statement prints 'Mary' again. Hence, the output is 'Mary - Mary'.
Tested: B.
IS CORRECT BECAUSE method check return the same object that is passed if no is null
B is correct