Given:
and
What is the result?
Given:
and
What is the result?
The given code initializes a Person object with the name 'Joe', invokes the checkPerson method on this object, and prints the object's state. Since checkPerson doesn't modify the original object reference, the name remains 'Joe'. The second part sets the object reference to null, and checkPerson creates a new Person object with the name 'Mary', although the returned new object reference is ignored, so the name remains null. Thus, the output is 'Joe' in the first print statement and 'null' in the second print statement.
b: public static void main(String[] args) { String s = "joe"; System.out.println(checkPerson(s)); s= null; System.out.println(checkPerson(s)); } public static String checkPerson(String s) { if(s==null) { s= "mary"; }else {s= null;} return s; } null mary
A is the correct Answer
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 is correct, check person never modified value for p so take the values from main method
A is correct
A is CORRECT
B. Correct
is correct A