Java Interview Question Set 10
Categories: Java 8(JDK1.8)
How can we access some class in another class in Java?
There are two ways to access a class in another class.
a) By using the fully qualified name: To access a class in a different package, either we must use the fully qualified name of that class, or we must import the package containing that class.
b) By using the relative path, We can use the path of the class that is related to the package that contains our class. It can be the same or subpackage.
Do I need to import java.lang package any time? Why?
No. It is by default loaded internally by the JVM.
Can I import same package/class twice? Will the JVM load the package twice at runtime?
One can import the same package or the same class multiple times. Neither compiler nor JVM complains about it. However, the JVM will internally load the class only once no matter how many times you import the same class.
What is the static import?
By static import, we can access the static members of a class directly, and there is no to qualify it with the class name.
There is given a list of exception handling interview questions with answers. If you know any exception handling interview question, kindly post it in the comment section.
How many types of exception can occur in a Java program?
There are mainly two types of exceptions: checked and unchecked. Here, an error is considered as the unchecked exception. According to Oracle, there are three types of exceptions:
a) Checked Exception: Checked exceptions are the one which are checked at compile-time. For example, SQLException, ClassNotFoundException, etc.
b) Unchecked Exception: Unchecked exceptions are the one which are handled at runtime because they can not be checked at compile-time. For example, ArithmaticException, NullPointerException, ArrayIndexOutOfBoundsException, etc.
What is Exception Handling?
Exception Handling is a mechanism that is used to handle runtime errors. It is used primarily to handle checked exceptions. Exception handling maintains the normal flow of the program. There are mainly two types of exceptions: checked and unchecked. Here, the error is considered as the unchecked exception.
What is the difference between Checked Exception and Unchecked Exception?
1) Checked Exception
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions, e.g., IOException, SQLException, etc. Checked exceptions are checked at compile-time.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked exceptions, e.g., ArithmeticException, NullPointerException, etc. Unchecked exceptions are not checked at compile-time.
What is the base class for Error and Exception?
The Throwable class is the base class for Error and Exception.
Is it necessary that each try block must be followed by a catch block?
public class Main{
public static void main(String []args){
try{
int a = 1;
System.out.println(a/0);
}
finally
{
System.out.println("rest of the code...");
}
}
}
Output:
rest of the code...
What is finally block?
The "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not. In other words, we can say that finally block is the block which is always executed. Finally block follows try or catch block. If you don't handle the exception, before terminating the program, JVM runs finally block, (if any). The finally block is mainly used to place the cleanup code such as closing a file or closing a connection. Here, we must know that for each try block there can be zero or more catch blocks, but only one finally block. The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).