Java interview questions on exception handling.

 Part 1

Java interview questions on exception handling covered in this post:


    Java interview questions on exception handling.

        What are the exceptions?

    -  Exception is an event, can be occurred during the execution of the program that disrupts the normal flow of the program.

    -   You can say an Exception is an abnormal event that can be occurred during the execution of the program.

    -      Exception can stop the execution of the program if not handled.

     

        What is the difference between exception and error?

     

    Error

    Exception

    1.

    Error is an invalid condition.

    An exception is an abnormal condition.

    2.

    An error cannot be handled.

    An exception can be handled.

    3.

    Error stops your program immediately.

    If you handle exceptions then your program may execute smoothly.

    4.

    An error can occur only run time but not compile time.

    An exception can occur compile time and run time.

    5.

    Error belongs to java.lang.Error

    Exeception belongs to java.lang.Exception.

    6.

    e.g. OutOfMemoryError

    e.g. NullPointerException, FileNotFoundException.

     

     

    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 checked exceptions and unchecked exceptions?

     

    Checked Exception

    Unchecked Exception

    1.

    Checked Exceptions can occur at Compile time.

    Unchecked Exceptions can occur at run time.

    2.

    If you don’t handle checked exceptions then your code will not compile. You will get a compile-time error.

    If you don’t handle unchecked exceptions you will get an exception at run time but your program will be compiled.

    3.

    Checked Exceptions extends Throwable and Error but except RuntimeException.

    Unchecked Exceptions extends RuntimeException.

    4.

    Examples :

    IOException, SQLException, FileNotFoundEx ception

    Examples:

    NullPointerException, ArithmeticException,

    ArrayIndexOutofBoundException

     

        How many types we can handle the exception?

    -          We can handle exceptions in two ways.

    1.       Using try and catch block.

    2.       Using throws keyword.

    -          Please refer to the below example to try-catch block.

    package simplifiedjava.crackedInterview; 

    public class Aeroplane {

          public void setLocation(String locationName) {

                try {

                      if(locationName.equals(null)) {

                            throw new NullPointerException();

                      }

                }catch(NullPointerException e) {

                      e.printStackTrace();

                }

          }    

    }

     

    -          Please refer to the below example for the throws keyword.

    package simplifiedjava.crackedInterview;

    public class Aeroplane { 

          public void setLocation(String locationName)throws NullPointerException {

                if(locationName.equals(null)) {

                      throw new NullPointerException();

                }          

          }    

    }



        Which is the parent class of the Exception hierarchy?

    -          Parent Class of Exception is Throwable.




        What is the difference between the throw and throws keyword? 

     

    Throw

    Throws

    1.

    Throw keyword can be used to throw an exception manually or explicitly.

    Throws keyword can be used to declare the list of exceptions.

    2.

    Throw can be declared inside a method.

    Throws can be declared in method definition followed by the parameter list.

    3.

    Only one Exception can be thrown with the throw keyword.

    Multiple Exceptions can be declared with the throws keyword.

    4.

    The checked exception cannot throw with the throw keyword.

    Checked and Unchecked exceptions can declare with the throws keyword.

     

        What is exception propagation?

    -    An Exception is thrown from the current method and if it is not handled then it drops to the previous method. If that exception is not handled in the previous method as well then it drops to the previous method and so on. If you never handle that exception then that exception reached to main method or base method which is called exception propagation.

    -     Please refer to the below diagram of exception propagation.

    Current Method  

    Exception occurred in this function and it is not handled.

    m4()

    Exception not handled in current method so dropped in method m4().

    m3()

    Exception not handled in m4() method so dropped in method m3().

    m2()

    Exception not handled in m3() method so dropped in method m2().

    m1()

    Exception not handled in m2() method so dropped in method m1().

    main() or base()

    Exception not handled in m1() so dropped in main() method and the program terminates abruptly.

     

        Can you explain the exception hierarchy? 

    -          Compile Time Exception or Checked Exception.

    1.       FileNotFoundException.

    2.       ClassNotFoundException.

    3.       NoSuchFieldException.

    4.       NoSuchMethodException.

    5.       InvalidClassException.

    6.       InvalidObjectException.

    7.       CloneNotSupportedException.

    8.       InterruptedException.

     

    -          Runtime Exception or Unchecked Exception.

    1.       NullPointerException.

    2.       ArithmeticException.

    3.       ClassCastException.

    4.       IndexOutOfBoundException.

    5.       ArrayIndexOutOfBoundException.

    6.       StringIndexOutOfBoundException.

    7.       IllegalStateException

    8.       IllegalMonitorStateException

     

    -          Error.

    1.       OutOfMemoryError.

    2.       StackOverFlowError.

    3.       IOError.

    4.       AssertionError.

     

        What is a multi-catch block? In which java version it has been introduced?

    -    In one catch block we can handle multiple exceptions is called a multi-catch block.

    -      Pipe Operator “|” can be used to segregate multiple exceptions.

    -      Multi catch block has been introduced in Java 7.

    -      Please refer to the below example for a multi-catch block.

    package simplifiedjava.crackedInterview; 

    public class Aeroplane { 

          public void setLocation(String locationName){

                try {

                            if(locationName.equals(null)) {

                                  throw new NullPointerException();

                            }

                }catch(NullPointerException | ArithmeticException e) {

                      e.printStackTrace();

                }

          }    

    }

     

        What is the role of the printStackTrace() method? This method belongs to which class?

    -    printStackTrace() method is used to diagnosing the exceptions.

    -   This method points out exactly where the exception occurred. It will print the function name and line number as well.

    -   printStackTrace() method belongs to the Throwable class which is the super parent class of the Exception hierarchy.

    -   For reference to the printStackTrace() method you can refer to the previous example.


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

    11. Can we create custom exception class. If yes then what are the steps to create customer exception class.

    12. Can you identify which try, catch and finally block combination is valid.

    13. Can we declare empty catch block.

    14. Can you explain why NullPointerException occurred and how to handle it?

    15. Can you explain why ClassCastException occurred and how to handle it?

    16. Can you explain why OutofMemoryError occurred and how to handle it?

    17. Can you explain why StackOverflowError occurred and how to handle it?

    18. Can you explain why ClassNotFoundError occurred and how to handle it?

    19. Can you explain why ExceptionInInitializerError occurred and how to handle it?

    20.what's the difference between stackoverflowerror and outofmemoryerror 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