Top 20 java interview questions and answers for freshers

Top 20 java interview questions and answers for freshers

In this article, we are going to share with our java aspirants the top 20 questions that are frequently asked in any java interview. These questions are basic questions that checks your understanding on the core and basic concepts of java.

You can see that duplicate element "Mukesh" is removed when List is converted to Set.

2. You are asked to define a list that would mostly involve in updation process like addition and deletion of elements frequently. Which list would you prefer for such tasks?

Linked list is mostly prefered in such scenarios.

Because for updation process like add and remove elements in java, if we use arraylist then lots of shifting needs to be carried out to complete the update process. This would be time consuming and makes the application slow.

But since linked list doesn't require shifting of elements so it provides faster operation while adding and removing elements in java.

3. What is the difference between arraylist and linkedlist in java?

Given below is the major difference between arraylist and linkedlist in java:

4. Why is iterator considered as fail-fast in java?

Iterator is considered as fail-fast as it immediately throws concurrent modification exception, if it senses that the collection on which it is iterating currently is attempted to be modified by any other thread from outside.

Fail fastfeatures ensures that if iterator feels that modification of collection would result in anamolous behaviour at any point of time in future, it fails immediately.

12. What is the difference between method overloading and method overriding in java?

Given below is the major difference between method overloading and method overriding in java:

Method overloading occurs when a method with same name is invoked with different parameters or parameters with different return type whereas method overriding occurs when sub-class implements method of super-class with exactly same type signature as its parent class.

Inheritance is not required for method overloading whereas method overriding occurs only in sub-class that inherits a super-class and tries to give its own specific implementation to parent class method.