Java Interview Questions and Answers for Beginners - 1
Explain Java Exception Hierarchy?
Java Exceptions are hierarchical and inheritance is used to categorize different types of exceptions. Throwable is the parent class of Java Exceptions Hierarchy and it has two child objects – Error and Exception. Exceptions are further divided into checked exceptions and run time exceptions.
Errors are exceptional scenarios that are out of scope of application and its not possible to anticipate and recover from them, for example hardware failure, JVM crash or out of memory error.
Checked Exceptions are exceptional scenarios that we can anticipate in a program and try to recover from it, for example FileNotFoundException. We should catch this exception and provide useful message to user and log it properly for debugging purpose. Exceptions is the parent class of all Checked Exceptions.
Runtime Exceptions are caused by bad programming, for example trying to retrieve an element from the Array. We should check the length of array first before trying to retrieve the element otherwise it might throw ArrayIndexOutofBoundException at runtime. Runtime Exception is the parent class of all runtime exceptions.
What happens when exception is thrown by main method?
When exception is thrown by main() method, Java RunTime terminates the program and print the exception message and stack trace in system console.
When is the finally clause of a try-catch-finally statement executed?
The finally clause of the try-catch-finally statement is always executed unless the thread of execution terminates or an exception occurs within the execution of the finally clause.
What class of exceptions is generated by the Java run time system?
The Java runtime system generates Runtime Exception and Error exceptions.
How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear.
The first catch clause that is capable of handling the exception is executed.
The remaining catch clauses are ignored.
7000+ Computer Networking MCQ Questions and Answers Pdf More
7000+ Microsoft Office Practical Questions and Answers Pdf More
7000+ Microsoft Office MCQ Questions and Answers Pdf More
7000+ Microsoft Office Quiz Questions and Answers Pdf More
7000+ Cyber Security Quiz Questions and Answers Pdf More
Top 100+ Sentence Completion Quiz More
RBI PO, SO, Clerk Computer Knowledge Questions with Answers More
100+ TNPSC Aptitude and Mental Ability Questions and Answers More
Computer Awareness Questions for BCA/MCA Entrance More
PG UG TRB Exams Question or Exam Pattern More