Top 50 Java Collections and Generics Interview Questions and Answers for 2 to 5 Years Experienced

Top 50 Java Collections and Generics Interview Questions and Answers for 2 to 5 Years Experienced

Hello friends , if you are preparing for Java Developer interviews then you may know that Java Collection and Generic are a very important topic for Java Interviews. If you want to crack the Java interview then you must prepare for Java collections and Generics questions.

In the past, I have shared best Java courses, books, and websites, and in this article, I am going to share 50 popular Java Collections and Generics questions form past Java Interviews.

As I said, its an important topic which you cannot afford to miss and these questions will give you the exposure of key concepts you need to know as a Java developers. They also present some of the hardest questions to a programmer when it comes to interviews, especially Generics.

It’s not easy to first understand what a particular piece of code doing with those question marks and other signs and then the pressure of interviews also makes it hard to answer complex usage of Generics.

But, with proper preparation and paying attention to both Java Collection and Generic, you can solve that hurdle. If you are looking for Java job but haven’t done well in the interviews you have given so far then you have come to the right place.

In this article, I have shared a lot of Java interview questions on various topics and difficulty levels. There are Java questions for beginners as well as expert programmers.

They are theoretical questions based upon Java programming concepts as well as coding and data structure algorithms questions for programmers, and this article is only going to make that collection even more valuable.

In this article, I am going to share some of the frequently asked Java Collection and Generic questions from Interviews.

These are the questions you have often seen on a telephonic round of Java interviews as well as on face-to-face interviews. It’s useful for both beginners having 2 to 3 years of experience as well as experienced Java programmers with 5 to 6 years of experience.

By the way, if you are serious about Java interview preparation, you can also checkout my books Grokking the Java Interview book and Grokking the Spring Boot Interview (use code friends20 to get 20% discount)

If you like to read on Kindle then, you can also buy the kindle version for just $9.9 on Amazon.

This list has a collection of questions that has both easy and tough questions in it but the most important thing is that most of the questions have already been asked in interviews. I am sure you might have also seen it in your interviews.

knowing the answers to these questions will not only help you to crack your Java interview but also understand Java Generics and Collection topic in-depth, which will eventually help you to write better Java programmers and code.

Btw, if you are new to Java or want to solidify your Java knowledge then you should check out a comprehensive course like The Complete Java Masterclass before attempting to solve these questions. It will help you immensely by filling gaps in your knowledge and going back and forth. It’s also the most up-to-date course and covers every new feature introduced in new Java releases

50+ Java Collection and Generic Interview Questions with Answers

Without wasting any more of your time, here is my list of 50+ Java interview questions on Collection and Generics.

If you have done some work in Java +then you should know the answer to these questions but if you don’t you can always see the answer.

Instead of writing answers here, I have linked them to relevant posts so that you can try to solve the problem by yourself here and if you need you can get an in-depth discussion on individual posts to learn the topic in depth.

1) What is the Java Collection Framework and How do you choose different collections? (answer)

Java Collection framework is a package in JDK which has classes implementing common data structure like dynamic array, linked list, hash table etc.

Here is the diagram which highlights all important classes from Java collection frameworks and when to use them. This Java collection cheat sheet is really good to quickly revise essential concepts.

And, if you want to learn more, you can also check?Introduction to Collections & Generics in Java course on Udemy. This course provides a simple guide to understand generics, basic collections, and reflection in Java!

2) What are Generics in Java? (answer)

hint: Java feature to ensure type safety at compile time.

3) Which are your favorite classes from Java Collection Framework? (answer)

hint: Collection, List, Set, Map, ArrayList, Vector, LinkedList, HashMap, etc

4) When do you use Set, List, and Map in Java? (answer)

hint — use set when you don’t need duplicates, use List when you need order with duplicates, and use Map when you need to store key-value pair.

5) Which Sorted Collection have you used? (answer)

hint — TreeSet is one example of a sorted Collection

6) How HashSet work in Java? (answer)

hint — same as HashMap, using hashing and equals() and hashCode() method. HashSet is actually backed by HashMap where keys are elements you store in HashSet and values are always null.

7) Which two methods you should override for an Object to be used as a key in hash-based Collections? (answer)

hint — equals and hashcode

8) Can you use HashMap in a concurrent application? (answer)

hint — Yes, but only if you are reading from the HashMap and its initialized by a single thread, otherwise no.

9) What is the difference between HashMap and Hashtable in Java? (answer)

hint — HashMap is fast but not threadsafe, Hashtable is slow but thread-safe

10) What is the difference between synchronized and concurrent Collection in Java? (answer)

hint - concurrent collections give better performance and they are more scalable as they use advanced locking mechanism like ConcurrentHashMap which is always better than Synchronized HashMap without only a portion of Map is locked at a time, instead of whole map.

11) How ConcurrentHashMap work in Java? (answer)

hint - partitions map into segments and lock them individually instead of locking the whole map.

12) What is PriorityQueue in Java? (answer)

A data structure that always keeps the highest or lowest element at the head so that you can access or remove it in constant time.

13) What is type-erasure in Generics? (answer)

It's a part of the Java compiler which removes all type-related information after compilation from Java so that the generated code is the same as before Generics.

14) What is the difference between ArrayList and Vector in Java? (answer)

hint — ArrayList is not synchronized hence fast, Vector is synchronized hence slow

15) What is the difference between LinkedList and ArrayList in Java? (answer)

hint — ArrayList is backed by array while LinkedList is backed by a linked list which means search with index is only possible in ArrayList.

16) What is the difference between Hashtable and ConcurrentHashMap in Java? (answer)

hint — ConcurrentHashMap is a new concurrent class with better scalability as only a portion of the map called segment is locked while Hashtable is an old class where the whole map is Locke for synchronization.

If you want to learn more, you can also see Introduction to Collections & Generics in Java?course on Udemy. Creates by Holczer Balazs, this course provides a simple guide to understand generics, basic collections, and reflection in Java! It

17) What is the difference between LinkedHashSet and TreeSet in Java? (answer)

hint — TreeSet is a sorted set where elements are stored in their natural or custom sorting order depending upon comparable and comparator while LinkedHashSet is just an ordered collection that maintains insertion order.

18) Difference between extends and super in Java Generics? (answer)

hint - different ways to implement Inheritance for example ArrayList<? extends Number> can hold Integers

19) What do you mean by thread-safe collection? Give an example of a 2 thread-safe Collection in Java? (answer)

hint - A collection which can be safely operated by multiple threads at the same time. ConcurrentHashMap and CopyOnWriteArrayList are thread safe collections

20) What is the relationship between equals and compareTo in Java? (answer)

hint - for two equal objects, compareTo must read 0

21) What is the default size of ArrayList and HashMap in Java? (answer)

hint - 16 but you can check JDK code

22) What is the load factor, capacity, and Size of the Collection in Java? (answer)

hint - percentage of map which should be filled before resize is triggered

23) What is the difference between Iterator and Enumeration in Java? (answer)

hint - both are used for traversal but one is new other is old. Also Iterator allows you to remove element from collection while traversal.

24) When does ConcurrentModificationException occur? (answer)

hint - when collection is modified during traversal other than Iterator's method, read about modCount also

25) What is the difference between fail-safe and fail-fast Iterator in Java? (answer)

hint - fail-fast iterator fails quickly and throws ConcurrentModificationException

26) What is CopyOnWriteArrayList in Java? (answer)

hint - A List implementation which copies whole list when modification is done.

27) When do you use BlockingQueue in Java? (answer)

hint - for implementing producer-consumer pattern in Java.

28) What is the difference between the peek() and poll() method of the Queue interface? (answer)

hint - peek() just return the head of the queue but doesn’t remove, but poll() not only return the element from the head of the queue but also removes it.

29) How do you find if an ArrayList contains an Object or not? (answer)

hint — you can use the contains() method to check if ArrayList contains a particularly object or not. It uses equals() method so it will only work if your object implements equals() otherwise, it will not work until you are passing the exactly same reference of the object.

30) Can we store Integer in an ArrayList<Number> in Java? (answer)

hint - No as its declared properly, if its ArrayList<? extends Number> then you can.

31) How get method of HashMap works in Java? (answer)

hint — hashing, hashcode method is used to find bucket location for putting the mapping and equals is used for retrieval.

32) How do you sort a Collection in Java? (answer)

hint - if its ordered collection like List then you can sort using Collections.sort() or List.sort() method.

33) What is the difference between ListIterator and Iterator in Java? (answer)

hint - ListIterator allows both way traversal I mean you can access both previous and next element.

34) What is the difference between HashSet and LinkedHashSet in Java? (answer)

hint - LinkedHashSet maintain orders, keys are kept in the order they are inserted.

35) When do you use EnumSet in Java? (answer)

hint - when you have to store finite number of elements

36) List down 4 ways to iterate over Map in Java? (answer)

hint —

  1. using for loop
  2. using for each loop of JDK 5
  3. using Iterator
  4. using ListIterator

If you want to learn more about using Collections then you can further see The Complete Java Masterclass course for more details. It’s also the most up-to-date course and covers every new feature introduced in new Java releases


37) How to create read-only Collection in Java? (answer)

hint - Immutable collections are read only, you can use List.of() to create immutable List.

38) What is IdentityHashMap in Java? (answer)

hint - A special collection which uses Object.equals() instead of normal equals() method to compare keys.

39) Difference between IdentityHashMap and WeakHashMap in Java? (answer)

hint - IdentityHashMap uses object reference to find keys while WeakHashMap help in garbage collection by allows GC to collect keys and values if there is no other refernece outside the WeakHashMap

40) What is the difference between Comparator and Comparable in Java? (answer)

hint - Comparator is used to implement custom order while Comparable is used to implement natural order like increasing or decreasing order of numbers or lexicographic order of String.

41) What is DeQueue? When do you use it? (answer)

hint - a data structure which allows insertion and deletion from both ends.

42) How do you remove an Object from the Collection? (answer)

hint - by using Iterators.remove() method or remove() method provided by Colelction

43) What is the difference between the remove() method of Collection and Iterator in Java? (answer)

hint - Iterator allows you remove current element.

44) What is the difference between ArrayList and ArrayList<?> in Java? (answer)

hint - former is used to store raw types while other is used to store parametric types. In case of later compile also check for type safety

45) What is the difference between PriorityQueue and TreeSet in Java? (answer)

hint - PriorityQueue allows you to access top priority element in constant time.

46) How can I avoid “unchecked cast” warnings? (answer)

hint - by using @SuppressWarning annotation.

47) What is the “diamond” operator in Java? (answer)

hint - <> is called diamond operator it helps with type inference and also help with less typing

48) What is the covariant method overriding in Java? (answer)

hint - tough question, see the answer

50) What is the difference between bounded and unbounded wildcards in Java generics? (answer)

hint - bounded wildcards allow only a selected object like ArrayList<? extends Number> will only allow Integer, Short or any Object which extends number while ArrayList<?> will allow all objects.

Java and Spring Interview Preparation Material

If you are preparing for Java and Spring developer interview then you can also checkout my books

Grokking the Java Interview:

Which contains many frequently asked Java question on popular topics with answers.

And, Grokking the Spring Boot Interview, which provides complete guide on preparing for spring developer interviews.

You can get your copy here — Grokking the Spring Boot Interview

That’s all in this list of 50 Java Generics and Collections Interview Questions and Answers for 2 to 5 years experience. They are a very important topic from the Java Interview point of view, especially collections. Make sure you prepare them well before going for any interviews. If you need further preparation you can also check out these Java Interview books and courses:

Further Learning

Java Interview Guide: 200+ Interview Questions and Answers

Java Programming Interview Exposed by Markham

Cracking the Coding Interview — 189 Questions and Answers

Data Structure and Algorithms Analysis for Job Interviews

Thanks for reading this article so far. If you like these Java Generics and Collections interview questions and answers then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. — If you are serious about mastering Java Collections — one of the most important Java API then I also suggest you check out the Java Bootcamp: Learn Java with 100+ Java Projects on Udemy. It’s a great course to learn why and which collections Java programmers should use.



Soma Sharma

Senior Java Developer at Freelancer

1 个月

If you want to master Java, learn different topics in depth and if you need resources, here are best Udemy Courses for Java Developers 1. Java Masterclass - https://bit.ly/3hOVyWI 2. Spring Boot 3 Masterclass - https://bit.ly/3VefuQE 3. Concurrency - https://bit.ly/3Wmpg4n 4. Hibernate - https://bit.ly/3FXALIS 5. Microservice - https://bit.ly/3Wd6FYB 6. DevOps - https://bit.ly/3WHJBRp

要查看或添加评论,请登录

Soma Sharma的更多文章

社区洞察

其他会员也浏览了