Java Interview Questions-Core Faq - 3
What's the base class of all exception classes?
The base class of all exception classes in Java is the java.lang.Throwable class.
Which are the two subclasses under the Exception class?
The two subclasses under the Exception class are RuntimeException (unchecked exceptions) and IOException (checked exceptions).
What is the difference between Checked Exception and Unchecked Exception?
Checked exceptions (e.g., IOException) are checked at compile time and must be either caught or declared in the method signature using the throws keyword. Unchecked exceptions (e.g., NullPointerException) are not checked at compile time and do not require explicit handling.
When ArithmeticException is thrown?
An ArithmeticException is thrown when an exceptional arithmetic condition occurs, such as dividing an integer by zero.
What is the difference between an error and an exception?
Errors (e.g., OutOfMemoryError) are severe problems that may occur in the application or the Java Virtual Machine, and they are not meant to be caught or handled by the application. Exceptions, on the other hand, are less severe and represent recoverable problems that can be caught and handled in the application.
Can the finally block be used without catch?
Yes, the finally block can be used without catch. It is used to ensure that code placed inside it gets executed regardless of whether an exception is thrown or not. However, if there is no catch block, the finally block will not handle any exceptions.
Which types of exceptions are caught at compile time?
Checked exceptions (e.g., IOException, SQLException) are caught at compile time. The compiler forces the programmer to either handle these exceptions or declare them in the method signature.
What happens if an exception is not handled in a program?
If an exception is not handled in a program, it will lead to termination of the program with a stack trace (exception details). The program will terminate abnormally, and the flow of execution will stop.
What is the difference between throw, throws, and Throwable?
throw is used to explicitly throw an exception from a method or a block of code. throws is used in a method declaration to indicate that the method might throw a specific type of exception, which needs to be handled by the caller. Throwable is the base class for all exceptions and errors in Java.
Does system.exit() in try block execute the finally block?
No, if System.exit() is called in the try block, the finally block will not execute. The program terminates immediately, skipping the finally block.
What causes a java.lang.ArrayIndexOutOfBoundsException, and how do I prevent it?
An ArrayIndexOutOfBoundsException is caused when you try to access an array element using an invalid index, i.e., an index that is outside the bounds of the array. To prevent it, make sure to check the array length and use valid indices when accessing array elements.
In case there is a return at the end of the try block, will it execute the finally block?
Yes, if there is a return statement at the end of the try block, the finally block will execute before the method returns. The finally block is guaranteed to execute regardless of whether there is a return in the try block or not.
What happens when you add a double value to a String?
When you add a double value to a String using the + operator, the double value will be converted to its string representation, and the concatenation will result in a new String.
When can the parseInt() method be used?
The parseInt() method in Java is used to parse (convert) a string representation of an integer into its numeric value. It can be used when you want to convert a String containing digits into an integer data type.
What value does read() return when it has reached the end of a file?
The read() method returns -1 when it has reached the end of a file. This value indicates that there are no more bytes to read from the file.
Can you serialize static fields of a class?
No, static fields are not serialized along with the object. Only the non-static (instance) fields of an object are serialized.
What is a serialVersionUID, and why should I use it?
serialVersionUID is a unique identifier associated with a serializable class in Java. It is used to ensure compatibility between serialized objects and their class versions, preventing potential deserialization errors when class structures change.
How do I convert a numeric IP address into a hostname?
You can use the InetAddress class in Java to convert a numeric IP address into a hostname. Use getHostName() method, passing the IP address as an argument, to obtain the corresponding hostname.
What's the simplest way to print a Java array?
The simplest way to print a Java array is by using the Arrays.toString() method from the java.util.Arrays class. It converts the array into a string representation, and you can directly print it to the console.