Given:
And the code fragment:
Which code fragment, when inserted at line 14, enables the code to print Mike Found?
A.
int f = ps.indexOf (p2)
B.
int f = ps.indexOf (Patient (“Mike”) );
C.
int f = ps.indexOf (new Patient “Mike”) );
D.
Patient p = new Patient (“Mike”);
Int f = ps.indexOf (p)
D – is the correct answer
The answer is A
Answer: A
package q029;
class Patient {
String name;
public Patient(String name) {
this.name = name;
}
}
package q029;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList ps = new ArrayList();
Patient p2 = new Patient(“Mike”);
ps.add(p2);
//insert code here
int f = ps.indexOf(p2);
if (f >= 0) {
System.out.println(“Mike Found”);
}
}
}
Output:
Mike Found
A. correct answer
B. NOT, doesn’t have the new keyword.
int f = ps.indexOf (Patient (“Mike”) );
C and D. NOT because
Patient p = new Patient (“Mike”);
Int f = ps.indexOf (p) create a new object reference. unless overwrite equals method in Patient class would work with this answers.
A