Globant Java interview questions and answers.

Java Interview Questions and Answers covered in this post:

    Java interview questions and answers

        Why main method is static and public?

    -   Main () method is the entry point of program execution. JVM calls the main method internally.

    -   If the method is declared as static then there is no need to create an object to call that method. JVM does not need to create an object to call the main() method.

    -    If the method is declared as public then that method is accessible outside the class and package. 


        What is the difference between instance method and static method?

     

    Instance Method

    Static Method

    1.

    No need to use any special keyword to declare Instance method.

    The Static method must declare with the static keyword.

    2.

    Class must be instantiated to invoke an instance method.

    No need to instantiate the class to invoke the static method.

    3.

    Instance method can be overloaded and overridden.

    The static method can be overloaded but cannot overridden.

    4.

    The Instance method can access the static variables as well as instance variables.

    The static method can access only Static variables but not instance variables.

     

     

    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 difference between relative path and absolute path?

    -  Absolute Path: Absolute path specifies the location from the root directory.

    -      E.g. E:/drawings/images/sky.image

    -  Relative Path: Relative path specifies the location from the current directory.

    -     E.g. images/sky.image


        Can you explain why StackOverFlowError occurred and how to handle it?

    -          StackOverFlowError occurs when the nesting function calls too deeply.

    -       Generally StackOverFlowError comes in recursion functions. Recursion means a function calls itself.

    -         To avoid StackOverFlowError you have to make sure recursive call must terminate somewhere.

    -          Please refer to the below example for StackOverFlowerror.

    package simplifiedjava.crackedInterview; 

    public class StackOverflowErrorDemo { 

          public static void main(String[] args) {

                recursiveFunction(1);

          }    

          public static void recursiveFunction(int no) {

              System.out.println("Number: " + no);

              if (no == 0)

                return;

              else

                recursiveFunction(++no);

            }  

    }

    Output: Exception in thread "main" java.lang.StackOverflowError

        What is the difference between Synchronous and Asynchronous programming?

    -          Implies that tasks will be executed one by one. The next task is started only after the current task is finished. Task3 is not started until task2 is finished.

    -    Implies that task returns control immediately with a promise to execute a code and notify about the result later(e.g. callback, feature). Task 3 is executed even if Task 2 is not finished.

    -    Please refer to the Real-Time Example.

    -    Synchronous: You are in a queue to get a movie ticket. You cannot get one until everybody in front of you gets one, and the same applies to the people queued behind you.

    -   Asynchronous: You are in a restaurant with many other people. You order your food. Other people can also order their food, they don't have to wait for your food to be cooked and served to you before they can order. In the kitchen, restaurant workers are continuously cooking, serving, and taking orders. People will get their food served as soon as it is cooked.


        What is the difference between CyclicBarrier and CountDownLatch?

    -  CyclicBarrier and Countdown latch techniques can be used in a multi-threaded environment to manage resources for multiple threads.

    -     The difference is explained below.

     

    CountDownLatch

    CyclicBarrier

    1.

    You cannot reuse the instance of CountDownLatch once the count reaches zero and the latch is open. CountDownLatch works like a count-down timer.

    CyclicBarrier can be reused by resetting the barrier once the barrier is broken.

    2.

    CountDownLatch doesn’t have any kind of Constructor.

    CyclicBarrier has a constructor where Runnable instance can be provided.

    3.

    CountDownLatch maintains the count of Tasks.

    CyclicBarrier maintains the count of Threads.

    4.

    The only current thread will throw InterruptedException. It will not impact other threads.

    If one thread is interrupted while waiting then other threads will throw BrokenBarrierException.


        What are the scenarios where InvalidClassException can be thrown?

    -          InvalidClassException can be thrown in following scenarios.

    1. The serial version of the class does not match that of the class descriptor read from the stream.

    2.   The class contains unknown data types.

    3.  The parent class does not have an accessible no-arg constructor while performing deserialization.


        What is the TreeSet? How the object will be stored in Treeset?

    -          TreeSet stores the object in sorted and ascending order.

    -          TreeSets underlying data structure is a balanced tree.

    -          Duplicate objects are not allowed.

    -          Insertion order not preserved.

    -          Homogeneous objects are allowed.

    -    Heterogeneous (Different types of Object) objects are not allowed otherwise we will get ClassCastException.

    -        Null insertion is not possible otherwise you will get NullPointerException.

    -  TreeSet is Serializable because TreeSet implements a Serializable interface.

    -       TreeSet is Clonable because TreeSet implements a Clonable interface.

    -       TreeSet has not implemented RandomAccess.

    -    All objects will be inserted based on some sorting order. It may be a default sorting order or customized sorting order.

    -      Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.Iterator;

    import java.util.TreeSet; 

    public class TreeSetDemo { 

          public static void main(String[] args) {

                TreeSet<Integer> set = new TreeSet<Integer>();

                set.add(50);

                set.add(10);

                set.add(30);

                set.add(20);

                set.add(40);

                System.out.println("Original Set : " + set);

                TreeSet<Integer> reverse = (TreeSet<Integer>) set.descendingSet();

                System.out.println("Reverse Set : " + reverse);           

                // Reverse using iterator

                Iterator<Integer> itr = set.descendingIterator();

                System.out.println("Using Iterator : ");

                while(itr.hasNext()) {

                      int i = (int)itr.next();

                      System.out.print(i+ ", ");

                }          

          }

    }

    Output:

    Original Set : [10, 20, 30, 40, 50]

    Reverse Set : [50, 40, 30, 20, 10]

    50, 40, 30, 20, 10,


        What is keySet?

    -          A map is a group of key-value pairs. The Set of keys are called keyset.

    -          Graphical representation of the key set.



        What is Hashtable? What are the characteristics of Hashtable?

    -          Hashtable is a thread-safe version of HashMap.

    -          Hashtable also maintain the elements in the form of Key/value pair.

    -          Underlying data structure of Hashtable is Hashtable.

    -          Insertion order never maintains.

    -          Insertion depends on the hashcode of the key.

    -          Duplicate keys are not allowed but duplicate values are allowed.

    -          Heterogeneous (Different types of Object) objects are allowed.

    -     Null is not allowed for both keys as well as value. Otherwise, it will through NullPointerException.

    -          Every method defined inside HashTable is synchronized.

    -    Hashtable is Clonable because Hashtable implements a Clonable interface.

    -          Hashtable is Serializable because Hashtable implements a Serializable interface.

    -       Hashtable is the worst choice if want to perform search or sort operation.

    -          Hashtable has not implemented RandomAccess.

    -          Please refer to the below example.

    package simplifiedjava.crackedInterview; 

    import java.util.Hashtable; 

    public class HashTableDemo { 

          public static void main(String[] args) {

                Hashtable<Integer, String> tMap = new Hashtable<Integer, String>();

                tMap.put(100, "Hundred");

                tMap.put(1, "One");

                tMap.put(1000, "Thousand");

                tMap.put(10, "Ten");

                System.out.println(tMap);          

          }

    }

    Output: {10=Ten, 1000=Thousand, 1=One, 100=Hundred}



    • 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:

    1. What is the difference between Compsition and Aggregation?

    2. Can we overload or override the main method.

    3. What is the difference between String s = "abc" and String s = new String("abc");

    4. What is SerialVersionUID. Who provides this SerialVersionUID. How will you create it.

    5. What is Annonymous Inner Class.

    6. What is HashMap and its internal working.

    7. Can we store objects in decending order in SortedMap. If yes then How can I store objects in decending order in SortedMap. 

    8. What is the difference between HashSet and LinkedHashSet.

    9. What is the difference between Traditional Collection and Concurrent Collection.

    10.How would you implement Multiple Inheritance in java?



    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