Top Java Interview Question and Answer 2023

Updated:01/01/2023 by Computer Hope

Top 50 Java Interview and Question

Q.1 How many types of memory areas are allocated by JVM
Class(Method) Area: Class Area stores per-class structures.
Heap: It is the runtime data area in which the memory is allocated to the objects
Stack: It holds local variables and partial results, and plays a part in method invocation and return.
Program Counter Register: program counter register contains the address of the Java virtual machine.
Native Method Stack: It contains all the native methods used in the application.

Q.2 what is Local variables ?
Ans: Local variables are those variables present within a block, function, or a constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable

Q.3 Why is Java not a pure object oriented language?
Ans: Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

Q.4 what is final variable?
Ans: final variable: When a variable is declared as final in Java, the value can’t be modified once it has been assigned. If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class.

Q.5 what is final method?
Ans: final method: A method declared as final cannot be overridden by its children's classes. A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here

Q.6 what is final class?
Ans: final class: No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage.

Q.7 what is final class?
Ans: final class: No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage.

Q.8 Can the static methods be overloaded?
Ans: Yes! There can be two or more static methods in a class with the same name but differing input parameters.

Q.9 Can the static methods be overridden?
Ans: No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases.

Q.10 What is the main objective of garbage collection?
Ans: The main objective of this process is to free up the memory space occupied by the unnecessary and unreachable objects during the Java program execution by deleting those unreachable objects.

Q.11 what is String Pool?
Ans: Designers of Java were aware of the fact that String data type is going to be majorly used by the programmers and developers. Thus, they wanted optimization from the beginning. They came up with the notion of using the String pool (a storage area in Java heap) to store the String literals. They intended to decrease the temporary String object with the help of sharing. An immutable class is needed to facilitate sharing. The sharing of the mutable structures between two unknown parties is not possible. Thus, immutable Java String helps in executing the concept of String Pool.

Q.12 What is the importance of reflection in Java?
Ans: The term reflection is used for describing the inspection capability of a code on other code either of itself or of its system and modify it during runtime.

Q.13 How to not allow serialization of attributes of a class in Java?
Ans: In order to achieve this, the attribute can be declared along with the usage of transient keyword as shown below:

Q.14 What happens if the static modifier is not included in the main method signature in Java?
Ans: There wouldn't be any compilation error. But then the program is run, since the JVM cant map the main method signature, the code throws “NoSuchMethodError” error at the runtime.

Q.15 Will the finally block be executed if the code System.exit(0) is written at the end of try block?
Ans: NO. The control of the program post System.exit(0) is immediately gone and the program gets terminated which is why the finally block never gets executed.

Q.16 What is the constructor?
Ans: The constructor can be defined as the special type of method that is used to initialize the state of an object. It is invoked when the class is instantiated, and the memory is allocated for the object. Every time, an object is created using the new keyword, the default constructor of the class is called. The name of the constructor must be similar to the class name. The constructor must not have an explicit return type.

Q.17 Can you make a constructor final?
Ans: No, the constructor can't be final.

Q.18 Can we overload the constructors?
Ans: Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. Consider the following example.

Q.19 What do you understand by copy constructor in Java?
Ans: There is no copy constructor in java. However, we can copy the values from one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in java.

Q.20 Which class is the superclass for all the classes?
Ans: The object class is the superclass of all other classes in Java.

Q.21 Why is multiple inheritance not supported in java?
Ans: To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class

Q.22 Why does Java not support pointers?
Ans: The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe(unsecured) and complex to understand.