class Person{
private String name;
public Person(String name) {
this.name = name;
}
public String toString() {
return name;
}
}
class Test {
public static void main(String[] args) throws IOException {
Person p = new Person("Joe");
checkPerson(p);
System.out.println(p);
p=null;
checkPerson(p);
System.out.println(p);
}
public static Person checkPerson(Person p) {
if (p == null) {
p = new Person("Mary");
} else {
p = null;
}
return p;
}
}
-> A
It's A.
If it would have been
p = checkPerson(p);
Then correct answer would have been answer Null and then Mary.
Now it's just checkPerson(p) so p remains "Joe" and then Null.
1th checkperson transforms joe to null , so system.out.print print null
2th checkperson recieve null and transforms this null to mary , so system.out.print print in the console "mary"
A is correct because Changes in references of Person p are in the Person class so that object has different scope So There will be no effect on the Tester class object.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
JGR_77
Highly Voted 1 year, 4 months agod7bb0b2
Most Recent 6 months, 4 weeks agoduydn
8 months, 4 weeks ago[Removed]
10 months, 2 weeks agogreat_chainick
10 months, 2 weeks agoJavi_sc
11 months, 4 weeks agoStavok
1 year agoaruni_mishra
1 year, 1 month agoMukes877
1 year, 2 months agoAlanRM
1 year, 3 months agoAnkit1010
1 year, 6 months ago