Java Collections interview questions and answers

Java Collections interview questions

In this post, I am going to share some of logical java Collections interview questions which will help to understand Collections concepts better. For an extensive list of java programs, please go to Java interview programs.

Question 1:
What can be output of below code :

options are : 
A)France France India
B)India India France France
C)India France
D)France France

Answer: A) France France India
Explanation:
As String class overrides hashcode and equals method, it won’t allow the same string twice in HashSet, but we did not implement hashcode and equals method for Country class, so each object will have different hashcode hence can be inserted in HashSet.

More explanation:
hashcode and equals method in java

Question 2:

What will be output of above program:
A. Compilation fails
B. 1
C. 2
D. An Exception is thrown at run time.

Answer: B. 1

Explanation :
As equals method always return true and hashcode return constant as 31. So when you try to put employeeTwo in HashSet when it will check for equals method, it will always return true, so employeeTwo won’t be added to HashSet

More explanation:
hashcode and equals method in java

Question 3:

What will be output of above program:
A. Compilation fails
B. 1
C. 2
D. An Exception is thrown at run time.

Answer: C.2

Explanation :
As we did not implement hashcode method, each object will have different hashcode(memory address) by default, so employeeTwo will also be added to employeeSet and size of HashSet will be 2

More explanation:
hashcode and equals method in java

Question 4:

What will be output of above program:
A. Martin
B. John
C. John
Martin
D. Compilation fails.
E. The code runs with no output.
F. An exception is thrown at runtime.
Answer: B. John

Explanation :
As you can see we have overridden compareTo method in Employee class and always return 0.
Following steps will take place:

  • First element with “John” will be added to employeeSet.
  • When we will added second element with martin, compareTo method will get called with employeeOne.compareTo(employeeTwo) and it will return 0.
  • As compareTo method returns 0, employeeOne is equals to employeeTwo, so employeeTwo will not be added to treeSet.
  • So output of above program is “John”

More explanation:
TreeSet in java

Question 5:

How to sort a collection of custom Objects in Java?</span
Answer :
We need to implement comparable interface to custom object class(Lets say Country) and then implement compareTo(Object o) method which will be used for sorting. It will provides default way of sorting custom objects.
If we want to sort custom object (Lets say country) on different attributes such as name,population etc.We can implement Comparator interface and can be used for sorting.
For more details,you can go through following links:

Question 6:

What will be output of below code :
options are:
A)Compile error : Integer can’t be added to the queue
B)Delhi Mumbai Pune 100
C)Delhi Mumbai Pune
D)Delhi Mumbai
Correct Answer: B. Delhi Mumbai Pune 100
Explanation:
As show method done not have any generic attached to it, integer can be also added to queue in show method.

Question 7:
Lets say you have class Employee as below:

You have list of employees, now you need to sort them on the basis of name then age. It means if names are equals, then sort it by age.
Answer:

Write a anonymous comparator to sort it by name then age.

So complete program will be :

When you run able program, you will get below output:

Question 8:
How HashMap works in java

I will recommend you to go to How HashMap works in java to understand it better.This is one of the most asked java collections interview questions.

Question 9:

What will be output of above program:
A. Compilation fails
B. 1
C. 2
D. An Exception is thrown at run time.

Answer: C. 2

Explanation :
As we did not override equals method here, it will have default implementation which return true if objects point to same reference (==) but here, as we have created two different object as employeeOne and employeeTwo, size of HashSet will be 2

More explanation:
hashcode and equals method in java

Question 10:
Difference between Comparator and Comparable in java?

Answer: This is also one of the most asked java collections interview questions. Please go to difference between comparator and comparable for differences.

Please go through java interview programs for more such programs and core java interview questions for more interview questions.

That’s all about java collections interview questions.
You may also like:

Was this post helpful?

Comments

  1. Hi

    Thanks for this wonderful site!
    I am little confused with first qn, since we didnt implement equals methos you said the answer was A)France France India, if thats the case then shouldnt the answer be B)India India France France rather than A? Can you please clarify?

Leave a Reply

Your email address will not be published. Required fields are marked *