Part 2
Interview questions on Collections in java for experienced covered in this post:
What is load factor in collection API? What is the default load factor?
-
Load factor is a measure that decides when to increase the
size of the collection.
-
Default load factor is 75%. It means when your collection
object gets full by 75% then collection size will increase the space
internally.
-
Every collection object has its own way or different formula
to increase the size internally.
What is the fill ratio in collection framework and what is the default fill ratio?
-
Fill ratio is a measure that decides when to increase the
size of the collection.
-
Default fill ratio is 75%. It means when your collection
object gets full by 75% then collection size will increase the space
internally.
-
Every collection object has its own way or different formula
to increase the size internally.
Why Collection interface doesn't extend Clonable and Serializable interface?
-
Collection interface is a root interface of the collection
hierarchy.
-
A lot of concrete implementation classes implements the
Collection interface.
-
If Collection implements Clonable and Serializable interface
then all concrete implementer classes mandatorily implement these two
interfaces.
-
If there is no requirement for these then every class has to
implement that functionality unnecessarily.
-
It may impact the performance or work redundancy may
happen.
-
To avoid all these problems Collections doesn’t implement
Clonable and Serializable interface
What are the characteristics of List Interface?
-
List is a child interface of the Collection interface.
-
Duplicates are allowed.
-
Insertion order preserved via index.
-
Index is used to retrieve elements.
-
Auto growable array.
What are the Implementation classes of List Interface?
-
List interface is the child interface of the Collection
interface.
-
List of implementation classes of List interface.
1.
ArrayList.
2.
LinkedList.
3.
Vector.
§
Stack.
What is ArrayList and what are the characteristics of ArrayList?
-
ArrayList is a growable array. No need to declare the size
like an array.
-
ArrayList is introduced in Java 1.2.
-
ArrayList is the best choice if you want to perform
retrieval, sorting and searching operations.
-
ArrayList is the worst choice if you want to perform an
insertion and deletion operation.
-
Characteristics:
1.
Resizable and Growable Array.
2.
Duplicate Allowed.
3.
Insertion Order Preserved.
4.
Heterogeneous (Different type of Object) Allowed.
5.
NULL insertion is possible.
6.
Not a Thread-safe.
7.
Arraylist is Serializable because this class implements
Serializable interface.
8.
Arraylist is Clonable because this class implements Clonable
interface.
9.
Randomly Accessible because Arraylist implements RandomAccess
interface.
What is the default size of ArrayList? What will happen if ArrayList gets fulled?
-
Default size of ArrayList is
10.
-
Once ArrayList reaches its max capacity then a new ArrayList
object will be created with the new size and copy all the elements from the
old ArrayList to the new ArrayList. Reference variable pointing to new
ArrayList and old ArrayList gets deleted.
-
Old ArrayList will be eligible for garbage collection.
-
Java people set some formula to create new space for array
object which is newly created.
-
New Capacity = ((Current Capacity * 3) / 2)+ 1
How will you convert Array to ArrayList and vice a versa?
-
Java has provided some readymade methods in utility class for
such kinds of operations.
-
We have Arrays and Collections utility classes for these
types of operations.
-
Arrays utility class has a couple of methods that convert the
array to ArrayList and ArrayList to array.
-
Convert Array to ArrayList we can use Arrays.asList()
method.
-
Convert ArrayList to Array we can use Arrays.toArray()
method.
-
Please refer to the below examples.
package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ConvertArrayListToArrayDemo { public static void main(String[] args) {
List<String> nameList
= new
ArrayList<String>();
nameList.add("Yogesh");
nameList.add("Arpita");
nameList.add("Shweta");
nameList.add("Shruti"); nameList.add("Rusty");
String[] updatedArray
= new
String[nameList.size()]; updatedArray = nameList.toArray(updatedArray);
for(String s
: updatedArray) {
System.out.println(s);
}
}
}
Output:
Arpita
Yogesh
Shweta Shruti package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ConvertArrayListToArrayDemo { public static void main(String[] args) {
List<String> nameList
= new
ArrayList<String>();
nameList.add("Yogesh");
nameList.add("Arpita");
nameList.add("Shweta");
nameList.add("Shruti"); nameList.add("Rusty");
String[] updatedArray
= new
String[nameList.size()]; updatedArray = nameList.toArray(updatedArray);
for(String s
: updatedArray) {
System.out.println(s);
}
}
}
Output:
Yogesh
Arpita
Shweta
Shruti
Rusty |
ArrayList is more flexible than Array but still where will you use Array
over Arraylist?
-
When you require the fixed size of homogeneous element then
we must go for an Array instead of an ArrayList.
-
Using Collection classes for primitives is appreciably slower
since they have to use autoboxing and wrappers.
-
We should prefer a more straightforward [] syntax for
accessing elements over ArrayList's get(). This becomes more important when
I need multidimensional arrays.
-
ArrayLists usually allocate about twice the memory you need
now in advance so that you can append items very fast. So there is wastage
if you are never going to add any more items.
-
ArrayList accesses are slower than plain arrays in general.
The ArrayList implementation uses an underlying array, but all accesses have
to go through the get(), set(), remove(), etc. methods which mean it goes
through more code than simple array access.
- 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