Exception and Thread Java Interview Questions
Categories: Programming
Q.1. What are the important methods of Java Exception Class?
Ans. Exception and all of it’s subclasses doesn’t provide any specific methods and all of the methods are defined in the base class Throwable.
(i) String getMessage() – This method returns the message String of Throwable and the message can be provided while creating the exception through it’s constructor.
(ii) String getLocalizedMessage() – This method is provided so that subclasses can override it to provide locale specific message to the calling program. Throwable class implementation of this method simply use getMessage() method to return the exception message.
(iii) Synchronized Throwable getCause() – This method returns the cause of the exception or null id the cause is unknown.
(iv) String toString() – This method returns the information about Throwable in String format, the returned String contains the name of Throwable class and localized message.
(v) void printStackTrace() – This method prints the stack trace information to the standard error stream, this method is overloaded and we can pass PrintStream or PrintWriter as an argument to write the stack trace information to the file or stream.
Q.2. What is a finally block? Is there a case when finally will not execute?
Ans. Finally block is a block which always executes a set of statements. It is always associated with a try block regardless of any exception that occurs or not.
Yes, finally will not be executed if the program exits either by calling System.exit() or by causing a fatal error that causes the process to abort.
Q.3. What is synchronization?
Ans. Synchronization refers to multi-threading. A synchronized block of code can be executed by only one thread at a time. As Java supports execution of multiple threads, two or more threads may access the same fields or objects. Synchronization is a process which keeps all concurrent threads in execution to be in sync. Synchronization avoids memory consistency errors caused due to inconsistent view of shared memory. When a method is declared as synchronized the thread holds the monitor for that method’s object. If another thread is executing the synchronized method the thread is blocked until that thread releases the monitor.
Q.4. What are the important methods of Java Exception Class?
Ans. Methods are defined in the base class Throwable. Some of the important methods of Java exception class are stated below.
String getMessage() – This method returns the message String about the exception. The message can be provided through its constructor.
public StackTraceElement[] getStackTrace() – This method returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack whereas the last element in the array represents the method at the bottom of the call stack.
Synchronized Throwable getCause() – This method returns the cause of the exception or null id as represented by a Throwable object.
String toString() – This method returns the information in String format. The returned String contains the name of Throwable class and localized message.
void printStackTrace() – This method prints the stack trace information to the standard error stream.
Q.5. What is OutOfMemoryError in Java?
Ans. OutOfMemoryError is the subclass of java.lang.Error which generally occurs when our JVM runs out of memory.
Q.6. What is a Thread?
Ans. A thread is the smallest piece of programmed instructions which can be executed independently by a scheduler. In Java, all the programs will have at least one thread which is known as the main thread. This main thread is created by the JVM when the program starts its execution. The main thread is used to invoke the main() of the program.
Q.7. What are the two ways to create a thread?
Ans. In Java, threads can be created in the following two ways:-
(i) By implementing the Runnable interface.
(ii) By extending the Thread
Q.8. Why does the java array index start with 0?
Ans. The decision to start array indexing from 0 in Java (and many other programming languages) is rooted in the design and implementation of these languages.
Imagine you have a list of items, like a grocery shopping list. In Java, when you store those items in an array (a container to hold multiple values), the first item in the list is referred to as item 0, the second item as item 1, and so on. The numbering starts from 0 instead of 1.
There are a few reasons for this. First, it helps keep things consistent and predictable. By starting from 0, it forms a clear pattern where each item’s position corresponds directly to its index. It’s like counting how many steps you take to reach a certain item, where the first step is step 0.
Another reason is that starting from 0 simplifies how arrays are stored in computer memory. It allows for efficient calculations and direct access to the memory location where an item is stored.
Finally, many programming languages, including Java, follow this convention because it’s familiar to developers who have worked with other languages like C and C++. It helps with compatibility and makes it easier to understand and share code across different languages.
So, while it might seem a bit unusual at first, starting array indexing from 0 is a convention that has been widely adopted in programming languages to provide simplicity, consistency, and efficiency.
Q.9. What are the different types of garbage collectors in Java?
Ans. Garbage collection in Java a program which helps in implicit memory management. Since in Java, using the new keyword you can create objects dynamically, which once created will consume some memory. Once the job is done and there are no more references left to the object, Java using garbage collection destroys the object and relieves the memory occupied by it. Java provides four types of garbage collectors:
(i) Serial Garbage Collector
(ii) Parallel Garbage Collector
(iii) CMS Garbage Collector
(iv) G1 Garbage Collector