Java Developer Interview Questions and Answers
There is a list of top frequently asked Java Interview Questions and Answers are given below.
1. What do you understand by Object and Class?
Ans – Object: In Java, objects are essentially a block of memory that contains space to store all the instance variables.
Class: A class is a user-defined data type with a template that serves to define its properties. A class is essentially a description of how to make an object that contains fields and methods.
2. What is meant by looping?
Ans – In Java, looping is a sequence of statements that executed until some conditions for the termination of the loop are satisfied.
3. Difference between Overloading and Overriding.
|1. It is performed at compile time||1. It is performed at runtime|
|2. It is carried out within a class||2. It is carried out with two classes
|3. It can perform overloading on static methods||3. It can't perform overriding on static methods|
|4. Overloaded methods use static binding||4. Overridden methods use dynamic binding|
4. What is method overloading?
Ans – In Java, it is possible to create methods that have the same name, but different parameter lists and different definitions that are called Method Overloading.
5. What is Inheritance?
Ans – In Java, here classes can be reused in several ways, this is done by creating new classes, reusing the properties of the existing class. This mechanism of deriving a new class from an old one that is called Inheritance.
6. Define a Vector class.
Ans – Vector is a type of class that can be used to create a generic dynamic array that can hold objects of any type and any number.
7. What is the function of ClassLoader?
Ans – ClassLoader is used to load class files before running the java program.
8. What is the function of a constructor?
Ans – Constructors are used to initializing the state of any object. When you create a new object using a new keyword, a default constructor is invoked. The constructor must have a name similar to the class name.
9. How do you use the ‘this’ keyword?
Ans – It invokes the current class method or class constructor. It also passes it on as an argument into methods or constructors.
10. What are JDK, and JRE?
Ans – JDK: The Java Development Kit (JDK) is a collection of tools that are used for developing and running programs. It includes appletviewer, javac, etc.
11. Difference between this() and super().
|1. this keyword holds the reference to current instance of a class.||1. super keyword always hold the reference to parent class of any given class.|
|2. this keyword allows to access variables and methods of the current class||2. super keyword allows to access to non-private (public, protected, or default) methods.|
12. What is an abstract class?
Ans – It indicates that a method must always be redefined in a subclass, this is done by using the abstract keyword in the method definition.
13. What are static methods and static variables?
Ans – Static Method is a method that is common to all the objects and accessed without using a particular object.
14. What is constructor overloading in java?
Ans – In Java, Constructor Overloading allows having more than one constructor inside one Class.
15. Difference between inner class and subclass.
|1. Inner classes are in the same file||1. Sub-classes are in another file|
|2. Inner class lies within the same class.||2. Sub-class resides as a child class.
|3. Inner classes have the methods they want.||3. Sub-classes have the methods of their parent class.|
Suppose, derived class (class A) inherits the base classes (class B and class C). The problem occurs if both base classes (class B and class C) have the same methods with the same signature. The compiler can’t determine which class method to be called. It creates ambiguity for the compiler. So, Java supports multiple inheritances.
17. What is an annotation?
Ans – Annotations are used to merge additional Java elements with the programming elements, such as classes, methods, parameters, local variables, packages, and fields. The annotations are also known as metadata.
18. Why Java doesn’t use pointers?
Ans – Java doesn’t use pointers because pointers are unsafe. Java uses reference types to hide pointers.
Otherwise, Memory allocation is done automatically it is managed by JVM, So to avoid direct access to memory by user pointers don’t allow in Java.
19. Define StringBuffer and StringBuilder
StringBuffer: It is a peer class of String. While String creates strings of fixed_length, StringBuffer creates flexible length that can be modified in terms of both length and content. It is used to insert characters and substrings in the middle of a string or append another string to the end.
StringBuilder: It is a mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class cannot be inherited.
public final class StringBuilder extends Object implements Serializable, CharSequence