Part 3
Java interview questions on exception handling covered in this post:
What is the difference between ClassNotFoundException and NoClassDefFoundError?
|
|
ClassNotFoundException |
NoClassDefFoundError |
|
1. |
ClassNotFoundException is checked exception. |
NoClassDefFoundError is an unchecked Exception. |
|
2. |
It is a child class of Exception class. |
It is a child class of Error class. |
|
3. |
This exception occurs when JVM is trying to load a class that is
not present in the classpath at runtime. |
This error occurs when JVM is trying to load a class that is not
present in classpath at run time but was present at compile
time. |
|
4. |
It occurs when the classpath is not updated with the required jar
files. |
It occurs when the required class definition is missing at
runtime. |
|
5. |
This exception thrown by Class.forName() or loadClass(). |
This error is thrown by Java Runtime System. |
Can we skip the finally block to execute? if yes then how?
- Yes we can skip the finally block to execute.
- We can skip using System.exit(0). Once you execute this method then your
program terminates.
- Please refer to the below example.
|
package simplifiedjava.crackedInterview; public class SkipFinallyBlockDemo {
public
static
void
main(String[] args) {
try
{
System.out.println("Inside try Block");
System.exit(0);
System.out.println("After Excecuting exit() method");
}catch(Exception e) {
System.out.println("Inside catch Block");
e.printStackTrace();
}finally
{
System.out.println("Inside finally Block");
}
}
}
Output: Inside try Block |
Can we have multiple catch blocks?
- Yes. You can have multiple catch block but there is some condition to
declared multiple catch blocks.
How should be the sequence of List of Exception classes in the catch
block?
-
When you are declaring multiple catch blocks then the child class must
declares first and then its parent and then parents parent and so on.
- Please refer to the below example for more clarity.
|
package
simplifiedjava.crackedInterview; public class MultipleCatchBlockDemo {
public
static
void
main(String[] args) { try {
}catch(ArrayIndexOutOfBoundsException ae) {
ae.printStackTrace();
}catch(IndexOutOfBoundsException ie) {
ie.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Explanation:
ArrayIndexOutOfBoundsException is a child class of
IndexOutOfBoundsException and IndexOutOfBoundsException is a child
class of Exception class. If you shuffle then you will get a
compile-time error. Most child classes should be first and then next
child and then parent class. |
What is the difference between final, finally and finalize?
|
|
Final |
Finally |
Finalize |
|
1. |
Final is a keyword. |
Finally is block. |
Finalize() is a method. |
|
2. |
Final keyword is applicable to class, method and variable. |
Finally block is applicable in exception handling with try or try
catch block. |
Finalize() method can be used in garbage collection. |
|
3. |
Final class cannot be sub-classed.
Final method cannot be overridden.
Final variable is not modifiable. |
Every time finally block will be executed exception is thrown or
not. An Exception is caught or not. If you execute System.exit(0)
then only the finally block won’t be executed. |
Finalize() method can run to release the resources acquired by
objects. After running finalize() method unreachable objects will be
eligible for garbage collection. |
Can we create a try block inside a try block?
-
Yes. You can have any number of nested try-catch blocks.
-
Please refer to the below example.
|
package simplifiedjava.crackedInterview; public class NestedTryCatchDemo {
public
static
void
main(String[] args) {
try
{
try
{
try
{
}catch(Exception e) {
e.printStackTrace();
}
}catch(Exception e) {
e.printStackTrace();
}
}catch(Exception e) {
e.printStackTrace();
}
}
} |
Can we write a try-catch block inside a catch block? What is the
purpose to write try block inside catch block?
- Yes. We can write a try-catch block inside the catch block.
- Generally, we are required to try/catch block inside catch block to close
the connections or close the statements or flush the data from statements
and connections.
What are the rules we must follow while throwing an exception in an
overridden method?
- If the superclass method does not declare an exception, the subclass’s
overridden method cannot declare the checked exception.
- If the superclass method does not declare an exception, the subclass’s
overridden method can declare an unchecked or runtime exception.
- If the superclass method declares an exception, the subclass’s overridden
method can declare the same exception or child class of that exception but
cannot declare the parent class of that exception.
- Please refer to the below examples for more clarity.
|
Parent Class Method |
Child Class Method |
Status |
|
Calculate() |
Calculate() throws IOException |
Not Allowed.
Reason: If the parent
class method doesn’t throw any exception then the child class method
should not through any checked exception. You can throw unchecked
exception. |
|
Calculate() |
Calculate() throws NullPointerException |
Allowed. |
|
Calculate() throws IOException |
Calculate() |
Allowed. |
|
Calculate() throws NullPointerException |
Calculate() |
Allowed. |
|
Calculate() throws ArithmeticException |
Calculate()throws Exception |
Not Allowed.
Reason: Child class
method cannot throw parent exception of Parent class. |
|
Calculate() throws Exception |
Calculate() throws Exception |
Allowed. |
What is an unreachable catch block error?
- Unreachable catch block error comes when we have multiple catch blocks in
exception handling.
- Catch block of Exceptions sequence must follow. Most child Exception
classes must be declared first and then its parent. Must be in ascending
order.
- Please refer to the below example.
|
package simplifiedjava.crackedInterview; import java.sql.Connection; public class NestedTryCatchDemo {
public
static
void
main(String[] args) { try { }catch(ArrayIndexOutOfBoundsException e) { }catch(IndexOutOfBoundsException e) { }catch(Exception e) {
}
} } Explanation:
If you observe in the above program. We have declared
ArrayIndexOutOfBoundsException exception first which is the child
class of IndexOutOfBoundsException which is the child class of
Exception. |
Does the finally block get executed if the try or catch block has a
return statement? How the control flows in this situation?
- Yes. finally{} block will be executed even though you have a return
statement in either try block or catch block.
- finally{} block will be executed prior to the return statement
execution.
- finally{} block will not be executed only in one condition. Once you invoke
System.exit(0) your JVM will be crashed and the finally{} block will not be
executed.
What is a re-throwing exception in java?
- Exception throws from try block and catches it in the catch block. A caught
exception that can be thrown from the catch block is called re-throwing the
exception.
- Re-thrown exception must handle somewhere in the program otherwise your
program will be terminated abruptly.
- Please refer to the below example.
|
package simplifiedjava.crackedInterview; public class NestedTryCatchDemo {
public
static
void
main(String[] args) {
try
{ int no = 10 / 0;
}catch(Exception e) {
throw
new
NullPointerException();
}
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at simplifiedjava.crackedInterview.NestedTryCatchDemo.main(NestedTryCatchDemo.java:10) |
What is the difference between NoSuchMethodError and
NoSuchMethodException?
- NoSuchMethodException is thrown when you try and get a method that doesn’t
exist with the reflection.
- NoSuchMethodError is thrown when the virtual machine cannot find the method
you are trying to call. This happens when a particular method was present at
compile time but not available at run time.
What is an AutoClosable resource?
- The resource is an object that must be closed after finishing the program.
- AutoClosable statement ensures that the resource is closed at
the end of the statement execution.
- Please refer below example.
|
package simplifiedjava.crackedInterview;
import
java.io.File;
import
java.io.FileNotFoundException; import java.util.Scanner; public class AutoClosableResourceDemo {
public
static
void
main(String[] args) {
Scanner scanner
= null;
try
{
scanner
= new
Scanner(new
File("Resume.doc"));
while
(scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
} catch
(FileNotFoundException e) {
e.printStackTrace();
} finally
{
if
(scanner
!= null) {
scanner.close();
}
}
}
} |
- 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