JAVA INTERVIEW QUESTIONS
Explain a situation where finally block will not be executed in Java?
A situation where finally block will not be executed in Java- Finally block will not be executed whenever jvm shutdowns. If we use system.exit(0) in try statementfinally block if present will not be executed.
Explain the importance of finally over return statement in Java?
Importance of finally over return statement in Java- finally block is more important than return statement when both are present in a program. For example, if there is any return statement present inside try or catch block , and finally block is also present firstfinally statement will be executed and then return statement will be…
Explain importance of throws keyword in java?
Throws statement is used at the end of method signature to indicate that an exception of a given typemay be thrown from the method.The main purpose of throws keyword is to delegate responsibility of exception handling to the callermethods, in the case of checked exception.In the case of unchecked exceptions, it is not required to…
Can we write any code after throw statement in Java?
After throw statement jvm stop execution and subsequent statements are not executed. If we try to write any statement after throw we do get compile time error saying unreachable code.
Explain throw keyword in java?
Generally JVM throws the exception and we handle the exceptions by using try catch block. But there aresituations where we have to throw userdefined exceptions or runtime exceptions. In such case we usethrow keyword to throw exception explicitly.Syntax : throw throwableInstance;Throwable instance must be of type throwable or any of its subclasses.After the throw statement…
What is default Exception handling in java?
When JVM detects exception causing code, it constructs a new exception handling object by including thefollowing information. 1) Name of Exception2) Description about the Exception3) Location of Exception. After creation of object by JVM it checks whether there is exception handling code or not. If there isexception handling code then exception handles and continues the…
Differences between checked and Unchecked exceptions in java?
Unchecked Exception Checked Exception All the subclasses of RuntimeException are called unchecked exception. All subclasses of Throwable class except RuntimeException are called as checked exceptions Unchecked exceptions need not be handled at compile time as These exceptions arise mostly due to coding mistakes in our program. Checked Exceptions need to be handled at compiletime. Here,…
What are unchecked exceptions in java?
Unchecked exceptions in javaAll subclasses of RuntimeException are called unchecked exceptions. These are unchecked exceptionsbecause compiler does not checks if a method handles or throws exceptions.Program compiles even if we do not catch the exception or throws the exception.If an exception occurs in the program, program terminates . It is difficult to handle these exceptionsbecause…
What are checked Exceptions in Java?
Checked Exceptions in Java1) All the subclasses of Throwable class except error, Runtime Exception and its subclasses are checkedexceptions.2) Checked exception should be thrown with keyword throws or should be provided try catch block, elsethe program would not compile. We do get compilation error.Examples :1) IOException,2) SQlException,3) FileNotFoundException,4) InvocationTargetException,5) CloneNotSupportedException6) ClassNotFoundException7) InstantiationException
What is the use of finally block in java?
Finally block in java-Finally block is used for cleaning up of resources such as closing connections, sockets etc. if try blockexecutes with no exceptions then finally is called after try block without executing catch block. If there isexception thrown in try block finally block executes immediately after catch block.If an exception is thrown, finally block…
Can we have multiple catch block for a try block in Java?
In some cases our code may throw more than one exception. In such case we can specify two or morecatch clauses, each catch handling different type of exception. When an exception is thrown jvm checkseach catch statement in order and the first one which matches the type of exception is execution andremaining catch blocks are…
Can we have try block without catch block in Java?
Each try block requires atleast one catch block or finally block. A try block without catch or finally, will result in compiler error. We can skip either of catch or finally block but not both.
What are try and catch keywords in java?
try and catch keywords in java- In try block we define all exception causing code. In java try and catch forms a unit. A catch block catches the exception thrown by preceding try block. Catch block cannot catch an exception thrown by another try block. If there is no exception causing code in our program…
List out five keywords related to Exception handling in Java ?
List of five keywords related to Exception handling in Java1) Try2) Catch3) throw4) throws5) finally
In how many ways we can do exception handling in java?
We can handle exceptions in either of the two ways :1) By specifying try catch block where we can catch the exception.2) Declaring a method with throws clause .
What are advantages of Exception handling in java?
1) Separating normal code from exception handling code to avoid abnormal termination of program.2) Categorizing in to different types of Exceptions so that rather than handling all exceptions withException root class we can handle with specific exceptions. It is recommended to handle exceptions withspecific Exception instead of handling with Exception root class.3) Call stack mechanism…
What is an error in Java?
Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environment issues such as running out of memory. In such cases we can’t handle the exceptions. Exceptions which cannot be recovered are called as errors…
What is Exception handling in java?
Exception handling is a mechanism what to do when some abnormal situation arises in program. When an exception is raised in program it leads to termination of program when it is not handled properly. The significance of exception handling comes here in order not to terminate a program abruptly and to continue with the rest…
State some situations where exceptions may arise in java?
1) Accessing an element that does not exist in array.2) Invalid conversion of number to string and string to number.(NumberFormatException)3) Invalid casting of class(Class cast Exception)4) Trying to create object for interface or abstract class(Instantiation Exception)
What is an exception in java?
In java exception is an object. Exceptions are created when an abnormal situations arise in our program. Exceptions can be created by JVM or by our application code. All Exception classes are defined in java.lang. In otherwords we can say Exception as run time error.