Menu
Question Index
...

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")));
    }
}


false false
true true
true
false
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: HashSet Class In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App