In this Java Interview Questions post, I am going to list down some of the important and mix of basic and advanced Java Interview questions which will increase your chances to pursue your career in Java programming.
Java Virtual Machine or JVM is a process that executes Java bytecode. Each of the Java source file is basically compiled into a bytecode file, which then executed by JVM. Java has the ability to allow a program to be built and run anywhere without having to write again or recompile for a separate platform. This is only possible through JVM because it is aware of the specific instructions and other specifications underlying the particular hardware platform.
Primitive data types which are supported by Java are as follows:
Java Runtime Environment or JRE is basically a Java Virtual Machine (JVM) where actual java programs get executed. Whereas the Java Development Kit or JDK is the package which featured software development kit for Java. JDK includes JRE, compilers and other tools which are essential in order to develop, compile, and execute Java programs.
When there are two or more methods in the same class having exact name but different parameters, then function overloading occurs. Whereas, when a child class redefines the same method as that of parents class then we call this case as function overriding.
This means that a method or a member variable can be accessed without having or requiring proper instantiation of the class where it belongs.
No. Because method overriding is based on dynamic binding at runtime only, while static methods bound at compile time.
Not at all. In Java, each class can extend only one class, while it can implement more than one interface.
In Java, we are able to create both abstract classes and interfaces and they share some common characteristics as well, but they differ in the following ways:
A process in Java, is an execution of a program, while Threads, on the other hand, is a single execution sequence within the process. A process can contain multiple threads.
Learn more about Multithreading in Java.
There are two major ways in Java to create a thread:
The Runnable interface is the preferred way of creating the thread as it does not require an object to inherit the Thread class. For complex applications, if there is a requirement for multiple inheritance then the only way is to pick Runnable interface.
New: This is the initiation phase when thread is created. Thread will reside in this state until the run method is not called off. It is also known as born thread.
Runnable: After the thread is created and once it starts using start () method then we call that state runnable with the usage of run () method. Thread in this state is considered to be executing its specific task.
Waiting: Sometimes a thread transition to the waiting state while it waits for another thread to perform a task. A waiting thread transitions back to the runnable state only when another thread notifies it to continue executing.
Timed Waiting: When a thread is in the runnable state it can enter in the timed waiting state for a specific time interval. A thread in this state goes back to the runnable state when the time interval expires or when the event for which it is waiting to occur.
Terminated: A running method ends it life when it completes its task. It happens by default.
To know more about states in threads, click here .
Both are interfaces in Java and perform same function i.e. traversing the Collection object. Enumeration is considered as fast and uses very less memory, whereas Iterator is safe as compared to Enumeration due to fact that thread do not able to modify the collection object which is currently traversed by Iterators.
In Java, Garbage Collection identifies and follows by discarding those objects which are no longer needed within the application, so that resources can be reused.
This method is called by garbage collector before release the memory of the object. It is a good practice to release resources which is held by the object inside finalize ().
There are two kinds of exceptions: Checked and Unchecked.
Checked exceptions are checked at compile time. If some piece of code or module in the application throws such exception then method must handle the exception or specify it by using throws keyword. While unchecked exceptions are not checked at compile time. Compiler will not complain if such exceptions occur. It is a good practice to specify or catch that particular exception.
Both exception and error are subclasses belong to Throwable class. In case of Error, a class defines exception that is not expected to be caught by the program, whereas Exception class is used for exceptional conditions that are to be caught by the program.
Applets are used to create interactive, user friendly and interactive web application. These are the Java programs that can be included in HTML code. Following is the lifecycle of an Applet:
Java Application is a standalone program that is executed outside of a browser while Applets need to be executed within the Java-enabled browser. In order to start their execution, Java Application requires the main method with a specific signature, while Applets don’t need such methods. However, both need the existence of JVM.
Deadlock is a condition where two processes are waiting for each other to get completed before proceeding further. This results in the process to wait endlessly.
Both of them implements Map interface and have many characteristics in common, but they differs in the following ways:
For more on this topic, click here.
No. It does not return anything. It is always declared with a void return type.
It is because Java uses eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.