Menu
Topics Index
...
`

Find Unique Names


If you need explanation Read this topic

If you need Answer Take test on this topic

Find Unique Names
What will be the output of the following program?
import java.util.*;
public class FindUniqueNames {
    public static void main(String[] args) {
        HashSet<Name> uniqueNames = new HashSet<Name>();
        p(uniqueNames, "kohli");
        p(uniqueNames, "Sachin");
        p(uniqueNames, "saCHIN");
        p(uniqueNames, "koHLi");
    }
    private static void p(Set<Name> uniqueNames, String name) {
        uniqueNames.add(new Name(name));
        System.out.print(uniqueNames.size() + " " + uniqueNames.contains(new Name(name)) + " ");
    }
}
class Name {
    String name;
    public Name(String name) { this.name = name; }
    public int hashCode() {    return name.toUpperCase().hashCode(); }
    public boolean equals(Object obj) {
        return name.equalsIgnoreCase(((Name) obj).name);
    }
    public String toString() { return name;    }
}

A. 1 false 2 false 3 false 4 false
B. 1 true 2 true 2 true 2 true
C. 1 true 2 true 3 true 4 true
D. Some other output
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics 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 is B. First kohli is added to the set and the size is 1. After adding it checks whether the given name is there or not. So as kohli is there in the set it prints true. Same is the case with Sachin. Next time when sachin is added it checks whether an object with same characters (case ignored) is present. So size is not increased and true is printed.

Posted by Nikhil Sulibhavi    2014-08-19 12:33:39


Answer is b since contains will return true if specified eelement exists.kohli and koHLi are both equal since their hash codes are equal and hence while inserting the koHli for th second  time,it is considered as dupicate

Posted by Shashanka Mogaliraju    2014-08-19 12:36:17


b..here both kohli n koHLi are equal sinc deir hash codes r eql n henc while imsrtng the koHli fr scnd timw it is considrd as duplicate

Posted by Asma Mujtaba Khan    2014-08-19 14:21:47


B

Posted by Rayapudi Ramya    2014-08-19 14:22:53


b.First kohli is added to the set and the size is 1. After adding it checks whether the given name is there or not. So as kohli is there in the set it prints true. Same is the case with Sachin. Next time when sachin is added it checks whether anobject with same characters (case ignored) is present. So size is not increased and true is printed.

kohli and koHLi are both equal since their hash codes are equal and hence while inserting the koHli for th second time,it is considered as dupicate

Posted by Nasir Khan    2014-08-19 14:27:58


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

Posted by Merit Campus    2014-08-20 05:05:08


how come nikhil sulibhavi be the winner of dis dose becoz he won th daily dose 451 c of monday n as per ur rules tje participants who have already won thid week r not eligible..plz chek into dis matter...immediately

Posted by Asma Mujtaba Khan    2014-08-20 05:33:17


chek dis.....

https://mbasic.facebook.com/java.meritcampus/photos/a.467782046626583.1073741828.467448966659891/714836711921114/?_e_pi_=7%2CPAGE_ID10%2C7991622014

Posted by Asma Mujtaba Khan    2014-08-20 05:35:23


One person can win only once in a week (from Monday to Friday), but for complex questions there is no such restriction. Nikhil Sulibhavi won complex questions 451C so he is eligible for this dose.

Posted by Merit Campus    2014-08-20 05:48:51


ohk...

Posted by Asma Mujtaba Khan    2014-08-20 05:55:34


© meritcampus 2019

All Rights Reserved.

Open In App