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:


import_contacts

You may also like:

Related Posts

  • Hibernate interview questions
    16 April

    Top 40+ Hibernate interview questions and answers

    Table of Contents1. What is ORM?2. What is Hibernate?3. What are advantages of Hibernate?4. Explain architecture of Hibernate?5. What are some core interfaces of hibernate?6. Explain brief about Session interface used in hibernate?7. Are session objects thread safe?8. Explain brief about SessionFactory object used in hibernate?9. What is Configuration class in hibernate?10. Explain brief about […]

  • Java Interview questions
    22 May

    Java interview questions

    Table of ContentsCore JavaCore java interview questionsCollections interview questionsMultithreading interview questionsJava 8 interview questionsOOPs interview questionsException Handling interview questionsString interview questionsSerialization interview questionsmethod overloading and method overriding interview questionsImmutable class interview questionsJava tricky interview questions Basic interview questions for fresher Interview questions for 5 to 7 years experiencedHow HashMap works in java? Difference between Interface […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym […]

  • Java interview questions for 2 years experience
    08 November

    Java interview questions for 2 years experience

    Table of Contents1.   Can we override static method in java?2.   Can you overload main method in java?3. Can we override private methods in java?4. What is the base class for all the classes?5.   Can you list down some of important method from object class?6.   Which two methods should you override while putting the custom object as […]

  • Java interview questions for 5 years experience
    08 November

    Java interview questions for 5 years experience

    Table of Contents1. Guess the output of below program.2. Guess the output of below program.3. Guess the output of below program.4. What will happen in case of below program?5. What will happen in case of below program?6. What will happen in case of below program?7. Guess the output of below program.8. Guess the output of […]

  • Java tricky interview questions
    11 September

    Java technical interview questions

    Table of Contents1 How HashMap works in java?2 Which methods you should implement for key Object in HashMap?3. Guess the output of below program.4. Guess the output of below code.5. What are differences between Comparator and Comparable?6. What is difference between Error and Exception?7. What is ClassLoader in java?8. What are types of ClassLoader in java?9. […]

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 *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.