Sunday, May 12, 2024

Java Interview Questions

 List of Top 15 Java Interview Questions for 2 years Experienced

Some of the Java interview questions for 2 years experience.

1. Tell me the difference between Method Overloading and Method Overriding in Java.

This is one of the core java interview questions for experienced professionals. Method Overloading

is used to increase the readability of the program.

Method Overloading has the same method name but different parameters. Method Overriding is

used for providing the specific implementation of the method. Method Overriding has the same

method and same parameters.

2. What do you mean by the class loader in Java?

In the Java Virtual Machine, the class loader is a type of subsystem. It is used for loading class files.

When you run the program, it will be first loaded by the class loader. The built-in class loaders are

of three types in Java-

•The Bootstrap Classloader is the first-class loader in JVM. It is the superclass of the

Extension class loader. It loads the rt.jar file that constitutes all the class files of Java

Standard Edition. The files being java.lang package classes, java.net package classes,

java.util packages classes, java.io packages classes, java.sql packages classes, etc.

•The Extension Classloader is the class loader of Bootstrap and the parent class loader of the

system classloader. It generally loads the jar files located inside the

$JAVA_HOME_/jre/lib/ext directory.

•The System Application Classloader is the type child class loader of the Extension class

loader. From the cross path, the System Application Classloader loads the class files. It is by

default; the classpath is set to the current directory. Therefore, it is also called the

Application class loader.

3. What do you mean by inheritance in Java?

To this Java interview question, you can answer it by saying; In Java, inheritance is a mechanism

through which an object acquires all the properties and behavior of another class. It is usually used

for Method Overriding and Code Reusability.

The concept of inheritance in Java is based on the fact that it creates new classes that are built upon

the existing classes. Therefore, these methods and fields of the parent class can be reused if we

inherit from an existing class. And also, we can add new methods and fields to our current class.

In Java, the inheritance is of five types-

•Hybrid Inheritance

•Hierarchical Inheritance

•Single-level Inheritance

•Multi-level Inheritance

•Multiple Inheritance

4. Why can we not override the static method in Java?The reasons why we cannot override the static method in Java are :

(a) The static method does not belong to the object level. Instead, it belongs to the class level. The

object decides which method can be called in the method overriding.

(b) In the static method, which is the class level method, the type reference decides on which

method is to be called without referring to the object. It concludes that the method that is called is

determined at the compile time.

If any child class defines the static method with the same signature as the parent class, then the

method in the child class hides the method in the parent class.

5. Can you tell us what the Dynamic Method Dispatch is in Java?

The Dynamic method dispatch is a process through which a call towards an overridden method is

solved at a run time. It is the object that is being referred to, not the type reference variable. It

decides which version of the overridden method needs to be executed.

6. What is a Java ClassPath?

A Java ClassPath is a type of environment variable which the Java Virtual Machine (JVM) uses to

collect all classes by the program.

7. What can be stated as a volatile keyword in Java?

If a variable will be marked as volatile, then the variable can be read from the main memory instead

of the cache memory.

8. Where does the final block not execute in Java?

To this Java interview question, you can answer it by saying; There is only one case where the final

block does not execute in Java. The final block does not execute when you run System.exit(0) in the

try or catch block in the Java programs.

9. What are the major points of distinction between StringBuffer and StringBuilder in Java?

A StringBuffer is thread-safe. Therefore, simultaneously two threads cannot call the methods of

StringBuffer. On the other hand, in comparison to StringBuffer, a StringBuilder is not known to be

thread-safe. Hence means that two threads can call the methods of StringBuilder at the same time.

On the basis of performance StringBuffer’s performance is less efficient as it is thread-safe.

Whereas StringBuilder’s performance is more efficient as it is not thread-safe.

10. Can you tell the difference between the Vector and ArrayList in Java?

You can answer this Java interview question by listing the differences between the vector and the

ArrayList.

(a) The common difference between the vector and the ArrayList in Java is that the vector is

synchronized and thread-safe while the ArrayList is not.

(b) Since vector is synchronized, it is slow, but since the ArrayList is not synchronized, it is

comparatively faster

11. Why is String immutable in Java?To this Java interview question, you can answer it by saying there are several reasons why String is

immutable or unchangeable in Java.

String pool- if you assign a value to String using the double quotes (” “), it gets stored in the string

literal pool area, and a single String can be referenced by several reference variables, which will

make it affect all reference variables if the String becomes mutable.

Classloading- String is used for the mechanism of class loading. If String becomes mutable, then it

will become a security threat because anyone can hack it.

Cache hash value-when you use String as a key in HashMap or any other collection, you can cache

its hash value. You do not need to calculate each time as it will always be constant because the

string is immutable.

12. Can you tell the difference between the HashMap and HashSet in Java?

To answer this ava interview question, HashMap implements the Map interface, which maps keys to

value. It is not synchronized, and it is not thread safe. Null keys and values are allowed, whereas

duplicate keys are not allowed.

HashMap<Integer,String> studentHashMap=new HashMap<Integer,String>();

studentHashMap.put(1, “Anushka”);

studentHashMap.put(2. “Akansha”);

In the case of HashSet, it implements a Set interface that does not allow duplicate values. As a

result, it is neither synchronized nor thread-safe.

HashSet studentSet=new HashSet();

studentSet.add(“Anushka”);

studentSet.add(“Akansha”);

studentSet.add(“Anjana”);

13. What is the Producer-Consumer Problem? What are the advantages of the Producer-

Consumer Pattern?

This is one of the advanced Java interview questions that is asked in a Java interview. The

Producer-Consumer Pattern can be implemented so that the Producer should wait if the bucket is

full. The Customer needs to wait if the bucket is empty. The classical way of solving the Producer-

Consumer Problem can be implemented by using the wait and notify method. It is to communicate

between Consumer and Producer threads and also blocks each of them on individual conditions

such as empty queue and full queue.

The Producer-Consumer Pattern is usually while writing the concurrent code and the multithreaded

code. The advantages of the Producer-Consumer Pattern are-

•The Producer does not need to know about the consumer or the number of consumers there

are. The same case goes with the Producer as well.

•The Producer and the Consumer can work at separate, different speeds. Usually, if you

monitor the speed of the consumer, then more consumers can be introduced for better

utilization.•If you functionally separate the Producer and Consumer, it can result in clean, readable, and

manageable code.

14. How do you think we can create an immutable class in Java?

An immutable class has only one state, and it is carefully instantiated by the constructor.

To create an immutable class in Java, you can follow these steps-

(a) You can make your class final so that no class will be able to extend that. Therefore, no one will

be able to override methods in this class.

(b) You can make your instance variables private, then any class will not be able to access instance

variables. You can make the final so that you cannot change it.

(c) You should not create a setter method for instance variables so that no explicit way will be there

to change the state of the instance variables.

(d) Initialize the variables in the constructor and take proper care while working with the mutable

object. You must do the deep copy in the case of mutable objects.

(e) You should return a clone of the object from the getter method so that it won’t return the original

object. After that, your original object will be intact.

15. Can you tell us the difference between the fail-safe and fail-fast iterator in java?

This is an advanced Java question for experienced professionals. To answer this question, the

difference between the Fail-fast iterator and fail-safe iterator in Java are-

fail-fast iterator- It fails as soon as they realize that the structure of the collection is changed since

the beginning of the iteration. The structural changes here are removing, adding, or updating any

element from the collection when one thread is iterating over that other collection. The fail-fast can

be implemented by maintaining a modification count. When the iteration thread will realize the

change in the count, it throws ConcurrentModificationException.

fail-safe iterator in Java- It does not throw an Exception when the collection is modified in a

structural manner while one iteration is working over it. It works on the clone collection instead of

the original collection

No comments:

Post a Comment