Top 32 Java Interview Questions for 2 to 3 Years Experience

In this post, I will be sharing the most frequently asked java interview questions for 2 to 4 years experienced. Relax, the expectation from the 2 to 4 years experienced guy is not the same as for senior java developers. I have categorized the interview questions into topics such as the OOPs concept, Collection framework, Exception Handling, String, Serialization, Java Multi-threading, and Coding basic questions. Please bookmark this page. I have added many links in the article so that it will be helpful for you to get a detailed explanation of the question. Let's dive deep into the interview questions with answers.

Java Interview Questions for 2 to 3 Years Experience


Q1 List down methods present in the Object class?

Below are the important methods present in the Object class:

a. clone(): this method is used to create a clone of the object
b. equals(): this method is used to check whether objects are equal.
c. hashCode(): this method returns the hash value of the object.
d. toString(): this method returns the String representation of the given object.
e. wait():  causes the current method to wait until another thread calls notify() or notifyAll() method.
f. notify(): wakes up the single thread waiting for this object's monitor.
g. notifyAll(): wakes up all the threads, waiting for this object's monitor.
h. finalize(): this method is invoked by garbage collector just before the object is garbage collected.

OOPS Concept


Q2 What is an interface?

In simple words, Interface is a blueprint of the class. It contains static constants and abstract methods.

Q3 What is the difference between Method Overriding and Method Overloading in Java?

Method Overloading is used to increase the readability of the program. Method Overriding is used to provide the specific implementation of the method. You can find a detailed explanation here.

Q4 Is it possible to override the static method in Java?

No, it is not possible to override the static method in Java. The reason is

a. The static method belongs to the class level, not the object level. In method overriding, it is the object that decides which method is to be called.

b. Also, for class-level methods i.e static methods, the type reference decides which method is called not the object being referred. It concludes the method called is determined at compile time.

If a child class defines a static method with the same signature as a static method in the parent class, then the method in the child class hides the method in the parent class.

Q5 What is the Dynamic method dispatch in Java?

Dynamic method dispatch is also known as Run time polymorphism. It is a mechanism by which a call to an overridden method is resolved at run time. At runtime, it is the type of object being referred to not the type reference of the variable that decides which version of an overridden method needs to be executed.

Exception Handling


Q6 What is the difference between Checked and Unchecked Exception in Java?

You can find a detailed explanation here.

Q7 What is the difference between throw and throws in Java?

Find the answer in detail here.

Q8 What is the try with resources statement in Java? 

According to Oracle docs, try-with-resources is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. It ensures that each resource is closed at the end of the statement.

Q9 Does finally block always execute in Java?

No, there is one scenario where the finally block does not execute. When you run System.exit(0) in the try or catch block, then finally block does not execute.

Collection


Q10 What is the difference between Array and ArrayList? Which is better?

The detailed answer to the difference between Array and ArrayList can be found here.

Q11 What is the difference between LinkedList and ArrayList in Java?

The detailed answer to the difference between LinkedList and ArrayList can be found here.

Q12 How HashMap works internally in Java?

This is one of the most important questions that every java developer must know. Find the detailed answer to How HashMap works internally in Java here.

Q13 What is the difference between fail-safe iterators and fail-fast iterators? 

The detailed answer to the difference between fail-fast and fail-safe can be found here.

Q14 What is the difference between HashMap and ConcurrentHashMap?

A detailed explanation of the difference between HashMap and ConcurrentHashMap can be found here.

Q15 What is the difference between HashMap and Hashtable?

The detailed answer to the difference between HashMap and Hashtable can be found here.

Q16 How ConcurrentHashMap works internally in Java?

A detailed explanation of ConcurrentHashMap internal working can be found here.

Multithreading


Q17 What is the difference between wait and sleep methods in Java?

You can find all the differences between the wait and sleep method here.

Q18 Explain the lifecycle of a Thread in java?

The lifecycle of a thread in java is explained in detail here.

Q19 What is the difference between Runnable and Thread in Java?

Differences between Runnable and Thread are explained here.

Q20 Which one is better to implement Runnable or to extends Thread?

implements Runnable is better than extends Thread. Using extends Thread we can not inherit any other class since Java does not allow multiple inheritance of classes.

String


Q21 What is the difference between String, StringBuilder, and StringBuffer in Java?

You can find the difference between String, StringBuilder, and StringBuffer here.

Q22 Why String is immutable in Java?

This is one of the important questions. You can find a detailed explanation here.

Q23 How many objects will be created in the below statement?

String strObj1 = "JavaHungry";
String strObj2 = "JavaHungry";

The answer is only 1 object is created. String strObj1 will create a new object in String constant pool whereas strObj2 will create the reference to the String strObj1.

Serialization


Q24 What is the marker interface? Name some marker interfaces in Java?

An empty interface i.e interface without methods and fields is called a marker interface in java. java.io.Serializable and java.lang.Cloneable are some of the examples of marker interface.

Q25 What is autoboxing and unboxing in Java?

Autoboxing can be defined as converting primitive data type (int, float, double, etc.) into their corresponding object wrapper classes.
for example: int to Integer, float to Float

Unboxing is the reverse of autoboxing. It converts Object wrapper classes into a primitive data types.

Q26 What is a transient keyword in Java?

transient keyword in Java is used to avoid Serialization.  If the variable is marked as transient then it will not be serialized.

Miscellaneous


Q27 What is the difference between final, finally, and finalize in Java?

The difference between final, finally and finalize is answered in detail here.

Q28 What are the different ways to call the garbage collector in Java?

There are two ways to call the garbage collector in Java.
a. System.gc()
b. Runtime.getRuntime().gc()

Q29 What is volatile keyword in Java?

If a variable is marked as volatile then this variable is read from the main memory instead of cache memory.

Q30 What are the features of Java 8?

This question is very important. You can find the features of Java 8 here.

Coding


Q31 How to reverse a string without using reverse() built-in method in Java? (Solution)

Q32 Write code for the producer-consumer problem in Java? (Solution)


That's all for today. Please mention in the comments in case you have faced Java interview questions for 2-4 years experienced that is not present in the above list.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry