Interview questions on Collections in java for experienced

 Part 1

Interview questions on Collections in java for experienced covered in this post:

    Interview questions on Collections classes in java for experienced


            What are Legacy Classes?

    -          A legacy class means old classes.

    -          Earlier versions of Java didn’t include the Collection framework.

    -          From java version 1.2 started using these Legacy classes. These classes were reengineered to support the collection interface. These classes are known as Legacy classes.

    -          Following are the Legacy classes.

    1.       Vector.

    2.       Stack.

    3.       Dictionary.

    4.       HashTable.

    5.       Properties.

     

            What is the purpose of Arrays utility class?

    -          Array utility class contained in java.util.Arrays package.

    -          This class provides some utility methods for manipulating the arrays like sorting, searching, converting an array into a collection etc.

     

     

    Click here to Purchase on AmazonClick here to Purchase on Amazon

    Click here to Purchase on Amazon

    Note: Please click on Image to Purchase the books from Amazon.in

            What is the purpose of Collections Utility class? What are the utility methods are available in Collections Class?

    -          Collections is a utility class for the collection framework.

    -          Collections utility class has some methods which are as follows.

    1.       addAll()

    2.       binarySearch()

    3.       checkedCollection​()

    4.       copy()

    5.       fill​()

    6.       min()

    7.       max()

    8.       replaceAll()

    9.       reverse()

    10.   shuffle()

    11.   synchronizedCollection()

    12.   unmodifiableCollection()

     

            What is the difference between Collection and Collections?

     

    Collection

    Collections

    1.

    The Collection is parent Interface.

    Collections is a utility class.

    2.

    It extends the Object class.

    It extends the Iterable interface.

      

            Is Collection object by default thread-safe?

    -          All collections are not thread-safe by default.

    -          Following collection objects are thread-safe rest are not thread-safe.

    1.       Vector.

    2.        HashTable.

    3.       ConcurrentHashMap.

    4.       CopyOnWriteArryList.

    5.       CopyOnWriteArraySet.

     

            How will you convert collection object to thread-safe collection object?

    -          We have a utility class that has some utility methods to convert non-thread-safe collections to the thread-safe collection.

    -          Each collection has its utility method to convert non-thread-safe to thread-safe.

    -          Following are the collections and its utility methods.

    1.       Collection has Collections.synchronizedCollection().

    2.       Map has Collections.synchronziedMap().

    3.       SortedMap has Collections.synchronizedSortedMap().

    4.       NavigableMap has Collections.navigableMap().

    5.       List has Collections.synchronizedList().

    6.       Set has Collections.synchronizedSet().

    7.       NavigableSet has Collections.synchronizedNavigableSet().

    8.       SortedSet has Collections.synchronizedSortedSet().

     

            How to remove the duplicates from list object without stream API?

    -          Couple of ways you can remove duplicates from the list.

    1.       By using HashSet.

    2.       By using LinkedHashSet.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.HashSet;

    import java.util.LinkedHashSet;

    import java.util.List;

    import java.util.Set; 

    public class RemoveDuplicatesFromList {

          public static void main(String[] args) {

                List<Integer> list = new ArrayList<Integer>();

                list.add(10);

                list.add(20);

                list.add(30);

                list.add(30);

                list.add(10);           

                System.out.println("----- Using HashSet-----");

                Set<Integer> listHashSet = new HashSet<Integer>(list);

                for(Integer i : listHashSet) {

                      System.out.println(i);

                }             

              System.out.println("----- Using LinkedHashSet-----");

              Set<Integer> listLinkedHashSet = new LinkedHashSet<Integer>(list);

                for(Integer i : listLinkedHashSet) {

                      System.out.println(i);

                }          

          }

    }

    Output:

    ----- Using HashSet-----

    20

    10

    30

    ----- Using LinkedHashSet-----

    10

    20

    30

     

            Is it possible to remove the duplicates and arrange all objects in sorting order in single step?

    -          Yes, It is possible to remove the duplicates and arrange all objects in sorting order using LinkedHashSet.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.LinkedHashSet;

    import java.util.List;

    import java.util.Set; 

    public class RemoveDuplicatesFromList { 

          public static void main(String[] args) {

                List<Integer> list = new ArrayList<Integer>();

                list.add(10);

                list.add(20);

                list.add(30);

                list.add(30);

                list.add(10);                       

               System.out.println("----- Using LinkedHashSet-----");

               Set<Integer> listLinkedHashSet = new LinkedHashSet<Integer>(list);

                for(Integer i : listLinkedHashSet) {

                      System.out.println(i);

                }          

          }

    }

    Output:

    ----- Using LinkedHashSet-----

    10

    20

    30

     

            Can you make collection object ready only? If yes then how will you make sure your collection object is read only?

    -          We can make collection read-only by unmodifiableList() method which exists in the Collections utility class.

    -          If you apply the unmodifiableList() method on any list object then you are not allowed to modify the list object. Still, you try to modify it then you will get UnSupportedOperationException.

    -          Make sure once you invoked to unmodifiableList() method on the list then you have to assign the same list to the old reference variable.

    -          If you don’t assign then you won’t get UnSupportedOperationException and you will be able to modify the list.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.ArrayList;

    import java.util.Collections;

    import java.util.List; 

    public class UnmodifiableListDemo { 

          public static void main(String[] args) {           

                List<Integer> list = new ArrayList<Integer>();

                list.add(10);

                list.add(20);

                list.add(30);

                list.add(30);

                list.add(10); 

                System.out.println("Before Unmodified "+ list);

                list = Collections.unmodifiableList(list);

                list.remove(2);        

                System.out.println("After Unmodified "+ list);

          }

    }

    Output:

    Exception in thread "main" Before Unmodified [10, 20, 30, 30, 10]

    java.lang.UnsupportedOperationException

           at java.util.Collections$UnmodifiableList.remove(Unknown Source)

                   at simplifiedjava.crackedInterview.UnmodifiableListDemo.main(UnmodifiableListDemo.java:21)

     

            When you will get UnSupportedOperationException?

    -          UnSupportedOperationException is a runtime exception.

             -        When you are trying to modify the unmodifiable list then you will get UnSupportedOperationException.



    • 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


    Upcoming questions in next post:


    Previous Post                                                                    Next Post



    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.

     

     





    Post a Comment

    0 Comments