Menu
Topics Index
...
`

Collections1980


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections1980
What will be the output of the following program?
import java.util.*;

public class MyNameIsKhan {
    private final String first, last;
    public MyNameIsKhan(String first, String last) {
        this.first = first;
        this.last = last;
    }
    public boolean equals(Object o) {
        if (!(o instanceof MyNameIsKhan))
            return false;
        MyNameIsKhan n = (MyNameIsKhan) o;
        return n.first.equals(first) || n.last.equals(last);
    }
    public static void main(String[] args) {
        Set<MyNameIsKhan> s = new HashSet<MyNameIsKhan>();
        s.add(new MyNameIsKhan("Shahrukh", "Khan"));
        System.out.println(s.contains(new MyNameIsKhan("Shahrukh", "Khan")));
    }
}

A. false false
B. true true
C. true
D. false
E. Some other output
F. Compilation Error or Runtime Error
Topic: HashSet Class In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans us D.. since we r creating another object MyNameIsKhan so objects are different it gives false..

Posted by Om Prakash Om    2014-05-20 09:28:18


ans d.contains() checks one collection contains all the membr of another,hashset not store values in fixed addres so contains method nt match - false

Posted by Maheshwari Natarajan    2014-05-20 09:45:06


Congratulations Om Prakash Om. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-21 04:09:33


© meritcampus 2019

All Rights Reserved.

Open In App