Part 4
Interview questions on Collections in java for experienced covered in this post:
Explain Big-O notation with an example?
-
The Big-O notation depicts the performance of an algorithm as
the number of elements in ArrayList.
-
A developer can use Big-O notation to choose the collection
implementation.
-
It is based on performance, time and memory.
-
For example, ArrayList get (index i) is a method to perform a
constant-time operation. It does not depend on the total number of elements
available in the list. Therefore, the performance in Big-O notation is O
(1).
What is Set?
-
Set is a child interface of Collection interface.
-
If we want to represent a group of individual objects as a
single entity where duplicates are not allowed and insertion order is not
preserved.
What are the implementation classes of Set Interface?
-
Following are the Classes which implements Set interface.
1.
HashSet.
2.
LinkedHashSet.
3.
TreeSet.
What is HashSet? Can you explain internal working of Hashset?
-
HashSet of Collection framework represents the group of
unique objects.
-
HashSet internally uses hashing technique to store the
elements. It's not guaranteed in which sequence the elements will be stored
or retrieved.
-
Whenever you insert an element into HashSet using add()
method, it creates an entry in the HashMap object with the element you have
specified as its key and constant called “PRESENT” as its value. This
“PRESENT” is defined in the HashSet class as below.
-
private
static
final
Object PRESENT
= new
Object();
-
Please refer the add method.
public
boolean
add(E e) {
return
map.put(e, PRESENT)==null;
-
}
- Please refer to the below example.
|
package
simplifiedjava.crackedInterview;
import
java.util.HashSet; public class HashSetDemo {
public
static
void
main(String[] args) {
HashSet<String> dayList
= new
HashSet<String>();
dayList.add("Sat");
dayList.add("Sun");
dayList.add("Mon");
dayList.add("Tue");
dayList.add("Sun");
System.out.println(dayList);
}
}
Output: [Tue, Sat, Sun, Mon] |
- Graphical Representation of Internal working of HashSet.
What are the characteristics of Hashset?
-
Duplicate objects are not allowed.
-
Insertion order not preserved.
-
Based on Hashcode of Object.
-
Only one null insertion is possible.
-
Heterogeneous (Different types of Object) objects are
allowed.
-
HashSet is Serializable because HashSet implements a
Serializable interface.
-
HashSet is Clonable because HashSet implements a Clonable
interface.
-
HashSet has not implemented RandomAccess.
-
Duplicate objects are not allowed and still, you are trying
to add duplicate objects then you won’t get any compile-time or run-time
exception just simply return false if the object is present and return true
when the object is not presented.
What is the default size and load factor of HashSet?
-
The default size of HashSet is 16.
-
Load Factor of HashSet is 75%.
Which data structure is implemented for HashSet?
-
Internally HashMap has been implemented for HashSet.
What is LinkedHashSet? What is the purpose of LinkedHashSet?
-
LinkedHashSet is a child class of HashSet but, LinkedHashSet
preserves the insertion order.
-
LinkedHashSet is a combination of LinkedList and HashSet.
-
You can say LinkedHashSet is the ordered version of
HashSet.
-
LinkedHashSet doesn’t have any method, it inherits all the
methods from its superclass i.e. HashSet.
-
If you want to maintain the insertion order then you can go
for LinkedHashSet.
Explain the internal working of LinkedHashSet?
-
LinkedHashSet internally uses the LinkedHashMap object to
store the elements.
-
The elements you inserted are stored as a key of the
LinkedHashMap object.
-
There are newly introduced two fields
before
and
after which are responsible for
maintaining the Insertion order in the LinkedHashSet object.
-
LinkedHashMap internally used doubly linkedlist to store the
elements.
-
Please refer to the below example.
|
package simplifiedjava.crackedInterview; import java.util.LinkedHashSet; public class LinkedHashSetDemo { public static void main(String[] args) {
LinkedHashSet<String> set
= new
LinkedHashSet<String>();
set.add("Sun");
set.add("Mon");
set.add("Tue");
set.add("Wed");
set.add("Sun");
System.out.println(set);
}
} |
- Please refer Graphical representation below.
What is SortedSet? What is the purpose of SortedSet?
-
If we want to represent a group of individual objects
according to some sorting order without duplicate then we should go for
SortedSet.
-
SortedSet interface is a child interface of the Set
interface.
-
Null is not allowed in SortedSet implementation.
-
All elements inserted into a sorted set must implement the
Comparable interface (or be accepted by the specified comparator).
-
Please refer to the below example.
|
package simplifiedjava.crackedInterview;
import
java.util.SortedSet; import java.util.TreeSet; public class SortedSetDemo {
public
static
void
main(String[] args) {
SortedSet<Integer> set
= new
TreeSet<Integer>();
set.add(50);
set.add(10);
set.add(30);
set.add(20);
set.add(40);
System.out.println(set);
}
}
Output: [10, 20, 30, 40, 50] |
-
Java interview questions and answers all MNC - Click here
-
Basic core java interview questions and answers for freshers
- Click here
-
Core java interview questions for 3 years experience - Click here
-
Core java interview questions and answers for 3-5 years exp
- Click here
-
Core java interview questions and Answers for 5 - 7 Years exp
- Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced
- Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
|
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on
some support projects and then I moved to java
projects. I had worked with many multi-national
companies like R-Systems, Tata Consultancy Services,
Cybage Softwares. Fortunately, TCS and
Cybage has given me an opportunity to take interviews
for experienced candidates. I have conducted more than
1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|

0 Comments