Top 30 Java Interview Questions & Answers
1. What is JAVA?
Answer: 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.
2. What is meant by the Local variable and the Instance variable?
Answer:
Local variables are defined in the method and scope of the variables that exist inside the method itself.
Instance variable is defined inside the class and outside the method and the scope of the variables exists throughout the class.
3. What is a Class?
Answer: All Java codes are defined in a Class. It 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.
4. What is the most important feature of Java?
Answer: Java is a platform independent language.
5. What do you mean by platform independence?
Answer: Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux, Solaris,etc).
6. What is a JVM?
Answer: JVM is Java Virtual Machine which is a run time environment for the compiled java class files.
7. Are JVM’s platform independent?
Answer: JVM’s are not platform independent. JVM’s are platform specific run time implementation provided by the vendor.
8. What is Inheritance?
Answer: Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class. The existing class is known as the Super class whereas the derived class is known as a sub class.
9. What is Encapsulation?
Answer: Purpose of Encapsulation:
- Protects the code from others.
- Code maintainability.
10. What is Polymorphism?
Answer: Polymorphism means many forms.
A single object can refer to the super-class or sub-class depending on the reference type which is called polymorphism.
11. What is JIT compiler?
Answer: Just-In-Time (JIT) compiler: It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
12. What are the main differences between the Java platform and other platforms?
Answer: There are the following differences between the Java platform and other platforms.
- Java is the software-based platform whereas other platforms may be the hardware platforms or software-based platforms.
- Java is executed on the top of other hardware platforms whereas other platforms can only have the hardware components.
- What gives Java its ‘write once and run anywhere’ nature?
The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform specific and can be executed on any computer.
13. If I don’t provide any arguments on the command line, then what will the value stored in the String array passed into the main () method, empty or NULL?
Answer: It is empty, but not null.
14. What if I write static public void instead of public static void?
Answer: The program compiles and runs correctly because the order of specifiers doesn’t matter in Java.
15. What is the default value of the local variables?
Answer: The local variables are not initialized to any default value, neither primitives nor object references.
16. Does constructor return any value?
Answer: yes, the constructor implicitly returns the current instance of the class (You can’t use an explicit return type with the constructor).
17. Is constructor inherited?
Answer: No, the constructor is not inherited.
18. Can you make a constructor final?
Answer: No, the constructor can’t be final.
19. Can we overload the constructors?
Answer: 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
20. What is the default value stored in Local Variables?
Answer: Neither the Local Variables nor any primitives and Object references have any default value stored in them.
21. What is a Class Loader?
Answer: A class loader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; Class Loader is the first to load the executable file.
Java has Bootstrap, Extension, and Application class loaders.
22. What are the Memory Allocations available in Java?
Answer: Java has five significant types of memory allocations.
- Class Memory
- Heap Memory
- Stack Memory
- Program Counter-Memory
- Native Method Stack Memory
23. What are the differences between Heap and Stack Memory in Java?
Answer: Stack memory is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program’s runtime.
24. Will the program run if we write static public void main?
Answer: Yes, the program will successfully execute if written so. Because, in Java, there is no specific rule for the order of specifiers.
25. What is the default value stored in Local Variables?
Answer: Neither the Local Variables nor any primitives and Object references have any default value stored in them.
26. What is an Association?
Answer: An Association can be defined as a relationship that has no ownership over another. For example, a person can be associated with multiple banks, and a bank can be related to various people, but no one can own the other.
27. What do you mean by aggregation?
Answer: The term aggregation refers to the relationship between two classes best described as a “whole/part” and “has-a” relationship. This kind is the most specialized version of an association relationship. It contains the reference to another class and is said to have ownership of that class.
28. What are Loops in Java? What are three types of loops?
Answer: Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of Java.
1) For Loops
Answer: For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when number of times to execute the statements is known to programmer.
2) While Loops
Answer: While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.
3) Do While Loops
Answer: Do While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once.
29. Why Java is not 100% Object-oriented?
Answer: 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.
30. What are wrapper classes in Java?
Answer: 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.