Accenture Interview Question


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

It will compile. Its not a good practice to return any constant number from hash code.
Please search for hashcode and equals method contract for better understanding

- santhosh May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There wont be any compile time or run time exceptions but i would never like my hashcode returning 0 . It is usually some distinct values which you would like to derive from some class level variables which are also defining the functioning of your equals method. (Note : this is an example) . Two equal objects should produce the same hash code .

- er.vishalmehta83@googlemail.com May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'd recommend you to read the concept of hashing.
Nevertheless, let me try to answer.
Hashing is generally used for fast retrieval, preferably in constant time.
If your hashCode always returns the same integer then all of your items will be inserted in one bucket. And when you try to access an item, it will locate the only bucket based on hashCode and then do a linear search in that only bucket to find the item. The time complexity will be O(n). Had you written a good hashCode method, the time complexity would have been constant.

- shaad May 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You are explaining hash function and not the hashCode function. The reason for hashCode function is that it helps in implementing the equals method. So, you compare 2 objects member by member iff the hashcode for the is same.

So, if we implement a good hashCode function then we will reduce the chances of doing full comparison in the cases where the objects aren't actually equal. If the hashCode keeps returning 0 then we will be doing full comparisons in all the cases and hence having implemented hashCode doesn't really solve any purpose.

- Pooja Gupta May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets, one string declare as String str=""; then the hashCode() return zero. The program will compile successfully. But it not good practice.

- Radhakanta Ghosh September 24, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More