Java Interview Questions And Answers

Q.1.What is JAVA?
A. Java is a high-level programming language and is platform independent.
Java is a collection of objects. It was developed by Sun Microsystems. There are a lot of applications, websites and Games that are developed using Java.
Q.2. What are the features in JAVA?
A. Features of Java:

  • Oops concepts
  • Object-oriented
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Platform independent: A single program works on different platforms without any modification.
High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called main thread. The user can create multiple threads by extending the thread class or by implementing Runnable interface.

Q.3.How does Java enable high performance?
A. Java uses Just In Time compiler to enable high performance. JIT is used to convert the instructions into bytecodes.
Q.4.What is a Class?
A. All Java codes are defined in a class. A Class has variables and methods.
Variables are attributes which define the state of a class.
Methods are the place where the exact business logic has to be done. It contains a set of statements (or) instructions to satisfy the particular requirement.
Q.5. What are the Oops concepts
A. Oops concepts include:

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Interface
Q.6.What is Inheritance?
A. Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class.
Existing class is known as Super class whereas the derived class is known as a sub class.
Q.7.What is meant by Method Overriding?
A. Method overriding happens if the sub class method satisfies the below conditions with the Super class method:

  • Method name should be same
  • Argument should be same
  • Return type also should be same
    The key benefit of overriding is that the Sub class can provide some specific information about that sub class type than the super class.
Q.8.How HashMap Works in Java
A. Hashmap is probably most discussed and controversial topic, if you are appearing in any junior or mid-level interview. You can face any interview question related to HashMap, if you know how hashmap works internally? This post will help you in answering some good questions like-

  • How HashMap store key-value pairs?
  • How HashMap resolve conflicts?
  • How hashCode() and equals() method are used in HashMap?
  • Impact of random/fixed hashCode() value for key?
  • Using HashMap in multi-threaded environment?
Q.9.What is abstraction in Java?
A. In the previous question, you learned polymorphism. Now it’s time to expand your knowledge by understanding abstraction as well. A very complicated topic for any Java interview.
Q.10. Difference between Interfaces and Abstract Classes?
A. There has been very clear separation abstract classes and interfaces in Java since the language was born. But a lot has changed since Java 8 release came in market. Its one of core concept was functional interfaces.
Functional interfaces completely changed the way we look at both basic building blocks of Java language. You cannot skip this question if your resume says you work on Java 8. In the linked tutorial, I will show you the correct scenarios which will help you in cracking some complex interview questions as well as case studies.
Q.11.Java Serialization and Serializable Interface
A. If you are preparing for Java interview with a Telecom company or any such domain who uses serialization in their application flows, then you will highly benefit from this tutorial. A very good list of do’s and dont’s with serialization in java. Possible questions may include –

  • What is serialVersionUID?
  • What is readObject and writeObject?
  • ow you will serialize and deserialize a class?
  • How you will make changes to a class so that serialization should not break?
  • Can we serialize static fields?
Q.12.How to Make a Java class immutable?
A. An immutable class is one whose state can not be changed once created. There are certain guidelines to create a class immutable in Java and you must know them to answer this question correctly.
Be aware that immutability is important in many design aspects and is a recommended design pattern by all Java gurus. Learn to make a java class immutable, how it benefits the application design and be prepared to encounter more software design interview questions on it.
Q.13. How to write a deadlock and resolve in Java
A. It can come in form of a puzzle. Better be ready for it. The interviewer may test your concurrency knowledge and your deep understanding on wait() and notify() method calls.
Be ready with one deadlock source-code example in your finger-tips. You will need it
Q.14. Compare and Swap [CAS] Algorithm
A. This question is targeted towards mid-level or senior developers. This requires a deep understanding of other concurrent concepts before answering this question. So It is a good way to test deep knowledge in Java concurrency.

  • What is optimistic and pessimistic locking?
  • What is compare and swap algorithm?
  • What is an atomic operation?
  • How AtomicInteger and AtomicLong works?
Q.15. What is meant by Ordered and Sorted in collections?
A. Ordered:It means the values that are stored in a collection is based on the values that are added to the collection. So we can iterate the values from the collection in a specific order.Sorted:Sorting mechanism can be applied internally or externally so that the group of objects sorted in a particular collection is based on properties of the objects.
Q.16. What is collection class in Java? List down its methods and interfaces.
A. In Java, the collection is a framework that acts as an architecture for storing and manipulating a group of objects. Using Collections you can perform various tasks like searching, sorting, insertion, manipulation, deletion, etc. Java collection framework includes the following:

  • Interfaces
  • Classes
  • Methods
Q.17. What is Polymorphism?
A. Polymorphism is briefly described as “one interface, many implementations”. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:

  1. Compile time polymorphism
  2. Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

Q.18. What are the different types of inheritance in Java?
A. Java supports four types of inheritance which are:

  1. Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will be only one parent as well as one child class.
  2. Multilevel Inheritance: When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.
  3. Hierarchical Inheritance: When a class has more than one child classes (subclasses) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.
  4. Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.
Q.19. What is method overloading and method overriding?
A.Method Overloading :

  • In Method Overloading, Methods of the same class shares the same name but each method must have a different number of parameters or parameters having different types and order.
  • Method Overloading is to “add” or “extend” more to the method’s behavior.
  • It is a compile-time polymorphism.
  • The methods must have a different signature.
  • It may or may not need inheritance in Method Overloading.

Method Overriding:  

  • In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters and same return type as a superclass.
  • Method Overriding is to “Change” existing behavior of the method.
  • It is a run time polymorphism.
  • The methods must have the same signature.
  • It always requires inheritance in Method Overriding.
Q.20. Can you override a private or static method in Java?
A. You cannot override a private or static method in Java. If you create a similar method with the same return type and same method arguments in child class then it will hide the superclass method; this is known as method hiding. Similarly, you cannot override a private method in subclass because it’s not accessible there. What you can do is create another private method with the same name in the child class.
Q.21. What is multiple inheritance? Is it supported by Java?
A. If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes.The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class.Therefore, Java doesn’t support multiple inheritance. The problem is commonly referred to as Diamond Problem.
Q.22. What is encapsulation in Java?
A. Encapsulation is a mechanism where you bind your data(variables) and code(methods) together as a single unit. Here, the data is hidden from the outer world and can be accessed only via current class methods. This helps in protecting the data from any unnecessary modification. We can achieve encapsulation in Java by:

  • Declaring the variables of a class as private.
  • Providing public setter and getter methods to modify and view the values of the variables.
Q.23Explain public static void main(String args[]) in Java.
A.main() in Java is the entry point for any Java program. It is always written as public static void main(String[] args).

  • public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
  • static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.
  • void: It is the return type of the method. Void defines the method which will not return any value.
  • main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
  • String args[]: It is the parameter passed to the main method.

r a function.

Q.24Why Java is platform independent?
A.Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.
Q.25Why Java is not 100% Object-oriented?
A.Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.
Q.26What are wrapper classes in Java?
A.Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.
Q.27What are types of constructors in Java?
A.In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.There are two types of constructors:

  1. Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation.
  2. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.
Q.28What is the difference between equals() and == in Java?
A.Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.
Q.29What is a package in Java? List down various advantages of packages.
A. Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:

  • Packages help in avoiding name clashes
  • They provide easier access control on the code
  • Packages can also contain hidden classes which are not visible to the outer classes and only used within the package
  • Creates a proper hierarchical structure which makes it easier to locate the related classes
Q.30Why pointers are not used in Java?
A. Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.
Q.31What is JIT compiler in Java?
A. JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.
Q.32What is final keyword in Java?
A. final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as:

  • final variable

When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

  • final method

When a method is declared final then it can’t be overridden by the inheriting class.

  • final class

When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.

Q.33What is constructor chaining in Java?
A. In Java, constructor chaining is the process of calling one constructor from another with respect to the current object. Constructor chaining is possible only through legacy where a subclass constructor is responsible for invoking the superclass’ constructor first. There could be any number of classes in the constructor chain. Constructor chaining can be achieved in two ways:

  1. Within the same class using this()
  2. From base class using super()
Q.34What is a classloader in Java?
A. The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is responsible for loading the class files. Whenever a Java program is executed it is first loaded by the classloader. Java provides three built-in classloaders:

  1. Bootstrap ClassLoader
  2. Extension ClassLoader
  3. System/Application ClassLoader
Q.35Why Java Strings are immutable in nature?
A. In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.