50 Java Interview Questions with Answers: Must Read Before Attending Technical Interview

50 most important Java Interview Questions for freshers and experienced candidates. You should go through it before facing any technical interview.

50 most important Java Interview Questions

1. What is the difference between JDK, JRE, and JVM?


JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which Java bytecode can be executed. It is a specification.

JVMs are available for many hardware and software platforms (so JVM is platform dependent).


JRE stands for Java Runtime Environment. It is the implementation of JVM.


JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

2. How many types of memory areas are allocated by JVM?

Many types:

Class(Method) Area
Program Counter Register
Native Method Stack

3. What gives Java its ‘write once and run anywhere’ nature?

The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

4. What is classloader?

The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders, e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

5. Is Empty .java file name a valid source file name?

Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let’s take a simple example:

//save by .java only
class A{
public static void main(String args[]){
System.out.println(“Hello java”);
//compile by javac .java
//run by     java A
compile it by javac .java

run it by java A

6. What is constructor?

The constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

7. What is the static variable?

Static variable is used to refer the common property of all objects (that is not unique for each object), e.g. company name of employees, college name of students, etc.
static variable gets memory only once in the class area at the time of class loading.

8. Difference between method Overloading and Overriding.

Method overloading increases the readability of the program. Whereas, Method overriding provides the specific implementation of the method that is already offered by its superclass.
Method overloading is occurred within the class. However, Method overriding occurs in two classes that have an IS-A relationship.
In Method Overloading, the parameter must be different. However, in method overrriding, the parameter must be the same.

9. What is the final Keyword In Java

Final is used in various contexts to define an entity that can only be assigned once.

  1. Once a final the variable has been assigned, it always contains the same value.
  2. If a final the variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object (this property of final is called non-transitivity).

Can you declare a final constructor
The final keyword in java is used to restrict the user. The java final keyword can be used in many contexts. Final can be:

  1. Variable
  2. Method
  3. Class

The final keyword can be applied to the variables, a final variable that have no value; it is called final blank variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.

10. Why can we not override static method?

It is because the static method is the part of the class and it is bound with class whereas the instance method is bound with object and static gets memory in class area and instance gets memory in heap.

11. What is an abstract class?

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

12. What is an interface in Java?

An interface is a blueprint of a class that has static constants and abstract methods. It can be used to achieve full abstraction and multiple inheritance.

13. Can an Interface be final?

No, because its implementation is provided by another class.

14. What is the difference between abstract class and interface?

  1. An abstract class can have a method body (non-abstract methods. Interface has only abstract methods.
  2. An abstract class can have instance variables. An interface cannot have instance variables.
  3. An abstract class can have a constructor. The interface cannot have a constructor.
  4. An abstract class can have static methods. The interface cannot have static methods.
  5. You can extend one abstract class. You can implement multiple interfaces.

15. What is the package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.

16. What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors. It is mainly used to handle checked exceptions.

17. 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.

18. What is the base class for Error and Exception?


19. Is it necessary that each try block must be followed by a catch block?

It is not necessary that a catch block must follow each try block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

20. What is difference between throw and throws?

throw keyword throws keyword
1. throw is used to explicitly throw an exception. throws are used to declare an exception.
2. checked exceptions cannot be propagated with throw only. checked exception can be propagated with throws.
3. throw is followed by an instance. throws are followed by class.
4. throw is used within the method. Throws is used with the method signature.
5. You cannot throw multiple exceptions You can declare a various exception, e.g. public void method()throws IOException, SQLException.

21) Why are string objects immutable in java?

Because Java uses the concept of the string literal. Suppose there are 5 reference variables, all refers to one object “Sachin.”If one reference variable changes the value of the object, it will be affected by all the reference variables. That is why string objects are immutable in java.

22. What is the fundamental difference between string and StringBuffer purposeobject?

String is an immutable object. Whereas, StringBuffer is a mutable object.

23. What is the difference between StringBuffer and StringBuilder?

StringBuffer is synchronized whereas StringBuilder is not synchronized.

24. What is Garbage Collection?

Garbage collection is a process of reclaiming the runtime unused objects.It is performed for memory management.

25. What is gc()?

gc() is a daemon thread.gc() method is defined in System class that is used to send a request to JVM to perform garbage collection.

26. What is the purpose of finalize() method?

finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

27. Can unreferenced objects be referenced again?


28. What kind of thread is the Garbage collector thread?

Daemon thread.

29. What is the difference between final, finally and finalize?

final: final is a keyword, final can be variable, method or class.You, can’t change the value of final variable, can’t override the final method, can’t inherit final class.
finally: finally block is used in exception handling. finally block is always executed.
finalize():finalize() method is used in garbage collection.finalize() method is invoked just before the object is garbage collected.The finalize() method can be used to perform any cleanup processing.

30. What is the purpose of the Runtime class?

The purpose of the Runtime class is to provide access to the Java runtime system.

31. How will you invoke any external process in Java?

By Runtime.getRuntime().exec(?) method.

32. What is serialization?

Serialization is a process of writing the state of an object into a byte stream.It is mainly used to travel object’s state on the network.

33. What is Deserialization?

Deserialization is the process of reconstructing the object from the serialized state. It is the reverse operation of serialization.

34. What is a transient keyword?

If you define any data member as transient,it will not be serialized.

35. What is Externalizable?

An externalizable interface is used to write the state of an object into a byte stream in the compressed format.It is not a marker interface.

36. What is the difference between Serializalble and Externalizable interface?

Serializable is a marker interface, but Externalizable is not a marker interface. When you use the Serializable interface, your class is serialized automatically by default. But you can override writeObject() and readObject() two methods to control more complex object serailization process. When you use Externalizable interface, you have complete control over your class’s serialization process.

37. What are wrapper classes?

Wrapper classes are classes that allow primitive types to be accessed as objects.

38. What is a native method?

A native method is a method that is implemented in a language other than Java.

39. What is the purpose of the System class?

The purpose of the System class is to provide access to system resources.

40. What comes to mind when someone mentions a shallow copy in Java?

Object cloning.

41. What is singleton class?

Singleton class means that any given time only one instance of the class is present, in one JVM

42. What is multithreading?

Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:

  1. Threads share the same address space.
  2. Thread is lightweight.
  3. Cost of communication between process is low.

43. What is the thread?

A thread is a lightweight subprocess. It is a separate path of execution. It is called a separate path of execution because each thread runs in a separate stack frame.

44. What is the difference between preemptive scheduling and time slicing?

Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

45. What does join() method?

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.

46. Is it possible to start a thread twice?

No, there is no possibility to start a thread twice. If we do, it throws an exception.

47. Can we call the run() method instead of start()?

Yes, but it will not work as a thread; instead, it will work as an ordinary object so there will not be context-switching between the threads.

48. What about the daemon threads?

The daemon threads are the low priority threads that provide the background support to the user threads. It provides services to the user threads.

49. Can we make the user thread as daemon thread if the thread is started?

No, if you do so, it will throw IllegalThreadStateException

50. What is synchronization?

Synchronization is the capability to control the access of multiple threads to any shared resource.It is used:

  1. To prevent thread interference.
  2. To prevent the consistency problem.



Leave a Reply