I am going to list some of the most important Java Interview Questions and Answers which will set you apart in the interview process.
Top 10 Java Interview Questions: 1. How many types of memory areas are allocated by JVM? Many types: 1.Class(Method) Area 2.Heap 3.Stack 4.Program Counter Register 5.Native Method Stack 2. What is a platform? A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides a software-based platform. 3. What is the main difference between the Java platform and other platforms? The Java platform differs from most other platforms in the sense that it’s a software-based platform that runs on top of other hardware-based platforms. It has two components: 1.Runtime Environment 2.API(Application Programming Interface) 4. What gives Java its ‘write once and run anywhere’ nature? The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform. 5. Why Java does not support pointers? 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. 6. Can I import the same package/class twice? Will the JVM load the package twice at runtime? One can import the same package or same class multiple times. Neither compiler nor JVM complains about it. But the JVM will internally load the class only once no matter how many times you import the same class. 7. What is JDBC? JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications. 8. What are Encapsulation, Inheritance, and Polymorphism? Encapsulation- It is the mechanism that binds together code and data it manipulates and keeps both safe from outside interference and misuse. Inheritance - It is the process by which one object acquires the properties of another object. Polymorphism - It is the feature that allows one interface to be used for general class actions. 9.What is method overloading and method overriding? Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding: When a method in a class having the same method name with the same arguments is said to be method overriding. 10. How do you print Array in Java? You can print an array by using the Arrays.toString() and Arrays.deepToString() method. Since array doesn’t implement toString() by itself, just passing an array to System.out.println() will not print its contents but Arrays.toString() will print each element. 11.The difference between sleep and wait in Java? Though both are used to pause currently running thread, sleep() is actually meant for short pause because it doesn’t release the lock, while wait() is meant for a conditional wait and that’s why it releases lock which can then be acquired by another thread to change the condition on which it is waiting. 12. What is Servlet Chaining? Servlet Chaining is the method where the output of one servlet is sent to a second servlet. The output of the second servlet can be sent to a third servlet, and so on. The last servlet in the chain is responsible for sending the response to the client. 13. What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard those objects that are no longer needed by the application, in order for the resources to be reclaimed and reused. 14. What is the importance of hashCode() and equals() methods? In Java, a HashMap uses the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More specifically, The HashCode method - It is used in order to determine where the specified key will be stored. Since different keys may produce the same hash value. The equals method - It is used, in order to determine whether the specified key actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and efficiency of the HashMap. 15. What is an Iterator? The Iterator interface provides a number of methods that are able to iterate over any Collection. Each Java Collection contains the iterator method that returns an Iterator instance. Iterators are capable of removing elements from the underlying collection during the iteration. Good luck with your programming interview! It’s certainly not going to be easy.