{"id":70885,"date":"2025-01-28T18:54:15","date_gmt":"2025-01-28T13:24:15","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=70885"},"modified":"2026-06-16T16:45:53","modified_gmt":"2026-06-16T11:15:53","slug":"interview-questions-for-java-developer-roles-2","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/interview-questions-for-java-developer-roles-2\/","title":{"rendered":"Top 100 Java Developer Interview Questions and Answers"},"content":{"rendered":"\n<p><strong>Quick Answer: <\/strong>This guide covers 100 Java developer interview questions across beginner, intermediate, advanced, and scenario-based levels. It helps you revise Java fundamentals, OOP, collections, multithreading, JVM concepts, Spring Boot use cases, and backend problem-solving with clear answers.<\/p>\n\n\n\n<p>Java was released by Sun Microsystems in 1995, but even after three decades, it still powers some of the world\u2019s most important backend systems, enterprise applications, Android apps, banking platforms, and cloud-based products.<\/p>\n\n\n\n<p>That is exactly why Java interviews are rarely limited to basic syntax. Recruiters want to know whether you can think like a real Java developer. They test your understanding of OOP, collections, exception handling, multithreading, memory management, JVM internals, Spring Boot scenarios, and production-level problem-solving.<\/p>\n\n\n\n<p>This guide brings together beginner, intermediate, advanced, and scenario-based Java developer interview questions and answers in one place. Each answer is written in a clear, technical, and interview-ready format so you can revise faster, explain concepts better, and walk into your next Java interview with more confidence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Beginner-Level Java Developer Interview Questions and Answers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. What is Java?<\/strong><\/h3>\n\n\n\n<p>Java is a high-level, object-oriented programming language used to build web applications, mobile apps, enterprise software, backend systems, and cloud-based applications.<\/p>\n\n\n\n<p>Java is popular because it is secure, platform-independent, scalable, and widely used in backend development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Why is Java Platform-Independent?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/getting-started-with-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a> is platform-independent because Java code is first compiled into bytecode. This bytecode can run on any system that has a Java Virtual Machine.<\/p>\n\n\n\n<p>This means a Java program written on Windows can also run on Linux or macOS without changing the source code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. What is the Difference Between JDK, JRE, and JVM?<\/strong><\/h3>\n\n\n\n<p><strong>JDK<\/strong> stands for Java Development Kit. It is used by developers to write, compile, and run Java programs.<\/p>\n\n\n\n<p><strong>JRE<\/strong> stands for Java Runtime Environment. It provides the required environment to run Java applications.<\/p>\n\n\n\n<p><strong>JVM<\/strong> stands for Java Virtual Machine. It executes Java bytecode and converts it into machine-level instructions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. What Are the Main Features of Java?<\/strong><\/h3>\n\n\n\n<p>The main features of Java are object-oriented programming, platform independence, security, robustness, multithreading, portability, and automatic memory management.<\/p>\n\n\n\n<p>These features make Java suitable for large-scale business applications and backend development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. What is Bytecode in Java?<\/strong><\/h3>\n\n\n\n<p>Bytecode is the intermediate code generated after Java source code is compiled.<\/p>\n\n\n\n<p>The Java compiler converts .java files into .class files. These .class files contain bytecode, which is executed by the JVM.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. What is the Difference Between Java and <\/strong><a href=\"https:\/\/www.guvi.in\/hub\/cpp\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>C++<\/strong><\/a><strong>?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Java<\/strong><\/td><td><strong>C++<\/strong><\/td><\/tr><tr><td>Runs on JVM<\/td><td>Compiles to machine code<\/td><\/tr><tr><td>Platform-independent<\/td><td>Platform-dependent<\/td><\/tr><tr><td>Automatic garbage collection<\/td><td>Manual memory management<\/td><\/tr><tr><td>No direct pointer access<\/td><td>Supports pointers<\/td><\/tr><tr><td>No multiple inheritance with classes<\/td><td>Supports multiple inheritance<\/td><\/tr><tr><td>Used for web, Android, backend apps<\/td><td>Used for systems, games, embedded apps<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. What is the Role of the <\/strong><strong>main()<\/strong><strong> Method in Java?<\/strong><\/h3>\n\n\n\n<p>The main() method is the starting point of a Java program.<\/p>\n\n\n\n<p>The JVM starts program execution from this method. A common Java main method is written as:<\/p>\n\n\n\n<p>public static void main(String[] args)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Why is Java Called a Strongly Typed Language?<\/strong><\/h3>\n\n\n\n<p>Java is called a strongly typed language because every variable must have a defined data type.<\/p>\n\n\n\n<p>A variable declared as an integer cannot directly store a string value. This reduces programming errors and improves code reliability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. What Are Variables in Java?<\/strong><\/h3>\n\n\n\n<p>Variables are containers used to store data in a Java program.<\/p>\n\n\n\n<p>Each variable has a name, data type, and value. Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int age = 25;\n\nString name = \"Rahul\";<\/code><\/pre>\n\n\n\n<p>Here, age stores a number, and name stores text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. What Are Primitive Data Types in Java?<\/strong><\/h3>\n\n\n\n<p>Primitive data types are the basic data types provided by Java.<\/p>\n\n\n\n<p>The eight primitive data types in Java are:<\/p>\n\n\n\n<p>byte, short, int, long, float, double, char, and boolean.<\/p>\n\n\n\n<p>They are used to store simple values such as numbers, characters, and true or false values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11. What is the Difference Between <\/strong><strong>==<\/strong><strong> and <\/strong><strong>.equals()<\/strong><strong> in Java?<\/strong><\/h3>\n\n\n\n<p>== checks whether two references point to the same memory location.<\/p>\n\n\n\n<p>.equals() checks whether two objects have the same value, depending on how the method is defined.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String a = new String(\"Java\");\n\nString b = new String(\"Java\");<\/code><\/pre>\n\n\n\n<p>Here, a == b is false because both objects have different memory locations. However, a.equals(b) is true because both values are the same.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12. What is Type Casting in Java?<\/strong><\/h3>\n\n\n\n<p>Type casting means converting one data type into another data type.<\/p>\n\n\n\n<p>Java supports two types of casting:<\/p>\n\n\n\n<p><strong>Implicit casting<\/strong> happens automatically when a smaller data type is converted into a larger data type.<\/p>\n\n\n\n<p><strong>Explicit casting<\/strong> is done manually when a larger data type is converted into a smaller data type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>13. What is the Difference Between Implicit and Explicit Type Casting?<\/strong><\/h3>\n\n\n\n<p>Implicit type casting is automatic and safe. Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int num = 10;\n\ndouble value = num;\n\nExplicit type casting needs manual conversion. Example:\n\ndouble value = 10.5;\n\nint num = (int) value;<\/code><\/pre>\n\n\n\n<p>Explicit casting can lead to data loss because decimal values may be removed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>14. What Are Java Operators?<\/strong><\/h3>\n\n\n\n<p>Java operators are symbols used to perform operations on variables and values.<\/p>\n\n\n\n<p>Common Java operators include arithmetic operators, relational operators, logical operators, assignment operators, and increment or decrement operators.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int sum = 10 + 5;<\/code><\/pre>\n\n\n\n<p>Here, + is an arithmetic operator.<\/p>\n\n\n\n<p>Prepare for Java developer interviews with confidence through HCL GUVI\u2019s <a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-beginners\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=top-100-java-developer-interview-questions-and-answers\" target=\"_blank\" rel=\"noreferrer noopener\">Java for Beginners Course<\/a>. Build a strong foundation in Java programming, object-oriented concepts, exception handling, collections, and core development skills through structured lessons and hands-on practice designed for aspiring software developers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>15. What is the Difference Between <\/strong><strong>break<\/strong><strong> and <\/strong><strong>continue<\/strong><strong>?<\/strong><\/h3>\n\n\n\n<p>break is used to stop a loop completely.<\/p>\n\n\n\n<p>continue is used to skip the current loop iteration and move to the next one.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= 5; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;if (i == 3) continue;\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(i);\n\n}<\/code><\/pre>\n\n\n\n<p>This skips printing 3 but continues the loop.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>16. What is a Constructor in Java?<\/strong><\/h3>\n\n\n\n<p>A <a href=\"https:\/\/www.guvi.in\/hub\/java-tutorial\/constructor-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">constructor<\/a> is a special method used to initialize an object.<\/p>\n\n\n\n<p>It has the same name as the class and does not have a return type.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\n\n&nbsp;&nbsp;&nbsp;&nbsp;Student() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Student object created\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>The constructor runs automatically when an object is created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>17. What is Constructor Overloading?<\/strong><\/h3>\n\n\n\n<p>Constructor overloading means creating more than one constructor in the same class with different parameters.<\/p>\n\n\n\n<p>It helps create objects in different ways.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\n\n&nbsp;&nbsp;&nbsp;&nbsp;Student() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Default constructor\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;Student(String name) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(name);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>18. What is the Difference Between a Constructor and a <\/strong><a href=\"https:\/\/www.guvi.in\/hub\/java-tutorial\/method-in-java\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Method<\/strong><\/a><strong>?<\/strong><\/h3>\n\n\n\n<p>A constructor is used to initialize an object. A method is used to perform an action or return a result.<\/p>\n\n\n\n<p>A constructor has the same name as the class and does not have a return type. A method can have any valid name and may return a value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>19. What is the <\/strong><strong>this<\/strong><strong> Keyword in Java?<\/strong><\/h3>\n\n\n\n<p>The this keyword refers to the current object of a class.<\/p>\n\n\n\n<p>It is commonly used when instance variables and local variables have the same name.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\n\n&nbsp;&nbsp;&nbsp;&nbsp;String name;\n\n&nbsp;&nbsp;&nbsp;&nbsp;Student(String name) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Here, this.name refers to the instance variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>20. What is the <\/strong><strong>static<\/strong><strong> Keyword in Java?<\/strong><\/h3>\n\n\n\n<p>The static keyword is used for members that belong to the class rather than a specific object.<\/p>\n\n\n\n<p>Static variables and methods can be accessed without creating an object of the class.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {\n\n&nbsp;&nbsp;&nbsp;&nbsp;static int count = 0;\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>21. What Are the Four Pillars of Object-Oriented Programming?<\/strong><\/h3>\n\n\n\n<p>The four pillars of object-oriented programming are encapsulation, inheritance, <a href=\"https:\/\/www.guvi.in\/blog\/what-is-polymorphism-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">polymorphism<\/a>, and abstraction.<\/p>\n\n\n\n<p>These concepts help developers write reusable, secure, modular, and maintainable Java code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>22. What is Encapsulation in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/what-is-python-encapsulation\/\" target=\"_blank\" rel=\"noreferrer noopener\">Encapsulation<\/a> means wrapping data and methods together inside a class.<\/p>\n\n\n\n<p>It is usually achieved by keeping variables private and accessing them through public getter and setter methods.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private int salary;\n\n&nbsp;&nbsp;&nbsp;&nbsp;public int getSalary() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return salary;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void setSalary(int salary) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.salary = salary;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>23. What is Inheritance in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/what-is-inheritance-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">Inheritance in Java<\/a> allows one class to use the properties and methods of another class.<\/p>\n\n\n\n<p>The class that inherits is called the child class. The class being inherited from is called the parent class.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void eat() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Eating\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\nclass Dog extends Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void bark() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Barking\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>24. What is Polymorphism in Java?<\/strong><\/h3>\n\n\n\n<p>Polymorphism means one action can behave in different ways.<\/p>\n\n\n\n<p>In Java, polymorphism is mainly achieved through method overloading and method overriding.<\/p>\n\n\n\n<p>Example of method overloading:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator {\n\n&nbsp;&nbsp;&nbsp;&nbsp;int add(int a, int b) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a + b;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;double add(double a, double b) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a + b;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>25. What is Abstraction in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/java-tutorial\/abstract-class\/\" target=\"_blank\" rel=\"noreferrer noopener\">Abstraction<\/a> means showing only important details and hiding internal implementation.<\/p>\n\n\n\n<p>In Java, abstraction can be achieved using abstract classes and interfaces.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class Vehicle {\n\n&nbsp;&nbsp;&nbsp;&nbsp;abstract void start();\n\n}<\/code><\/pre>\n\n\n\n<p>Here, the start() method is declared, but its internal logic is provided by child classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>26. What is the Difference Between Abstraction and Encapsulation?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Abstraction<\/strong><\/td><td><strong>Encapsulation<\/strong><\/td><\/tr><tr><td>Hides implementation details<\/td><td>Hides internal data<\/td><\/tr><tr><td>Focuses on what an object does<\/td><td>Focuses on how data is protected<\/td><\/tr><tr><td>Achieved using abstract classes and interfaces<\/td><td>Achieved using private variables and getter\/setter methods<\/td><\/tr><tr><td>Reduces code complexity<\/td><td>Improves data security<\/td><\/tr><tr><td>Example: Vehicle.start()<\/td><td>Example: private salary field<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>27. What is Method Overloading?<\/strong><\/h3>\n\n\n\n<p>Method overloading means having multiple methods with the same name but different parameters in the same class.<\/p>\n\n\n\n<p>It improves code readability and supports compile-time polymorphism.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MathOperation {\n\n&nbsp;&nbsp;&nbsp;&nbsp;int sum(int a, int b) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a + b;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;int sum(int a, int b, int c) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a + b + c;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>28. What is Method Overriding?<\/strong><\/h3>\n\n\n\n<p>Method overriding happens when a child class provides its own version of a method already defined in the parent class.<\/p>\n\n\n\n<p>It supports runtime polymorphism.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void sound() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Animal sound\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\nclass Dog extends Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void sound() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Dog barks\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>29. What is the Difference Between Overloading and Overriding?<\/strong><\/h3>\n\n\n\n<p>Overloading happens in the same class. It uses the same method name with different parameters.<\/p>\n\n\n\n<p>Overriding happens between a parent class and a child class. The child class changes the behavior of a parent class method.<\/p>\n\n\n\n<p>Overloading is compile-time polymorphism. Overriding is runtime polymorphism.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>30. What is Dynamic Method Dispatch in Java?<\/strong><\/h3>\n\n\n\n<p>Dynamic method dispatch is the process where the method call is resolved at runtime.<\/p>\n\n\n\n<p>It happens when a parent class reference points to a child class object.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void sound() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Animal sound\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\nclass Dog extends Animal {\n\n&nbsp;&nbsp;&nbsp;&nbsp;void sound() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Dog barks\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\nAnimal obj = new Dog();\n\nobj.sound();<\/code><\/pre>\n\n\n\n<p>Here, obj.sound() calls the Dog class method at runtime. This is dynamic method dispatch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Intermediate-Level Java Developer Interview Questions and Answers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>31. What is the Java Collection Framework?<\/strong><\/h3>\n\n\n\n<p>The Java Collection Framework is a set of classes and interfaces used to store, manage, and process groups of objects.<\/p>\n\n\n\n<p>It includes commonly used structures such as ArrayList, LinkedList, HashSet, TreeSet, HashMap, and Queue. Java developers use collections to handle data more efficiently in backend applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>32. What is the Difference Between List, Set, and Map in Java?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>List<\/strong><\/td><td><strong>Set<\/strong><\/td><td><strong>Map<\/strong><\/td><\/tr><tr><td>Stores ordered elements<\/td><td>Stores unique elements<\/td><td>Stores key-value pairs<\/td><\/tr><tr><td>Allows duplicates<\/td><td>Does not allow duplicates<\/td><td>Keys must be unique<\/td><\/tr><tr><td>Accessed by index<\/td><td>No index-based access<\/td><td>Accessed by key<\/td><\/tr><tr><td>Example: ArrayList<\/td><td>Example: HashSet<\/td><td>Example: HashMap<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>33. What is the Difference Between ArrayList and LinkedList?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>ArrayList<\/strong><\/td><td><strong>LinkedList<\/strong><\/td><\/tr><tr><td>Uses dynamic array internally<\/td><td>Uses doubly linked list internally<\/td><\/tr><tr><td>Faster for searching<\/td><td>Faster for frequent insertion\/deletion<\/td><\/tr><tr><td>Better for read-heavy tasks<\/td><td>Better for update-heavy tasks<\/td><\/tr><tr><td>Access by index is faster<\/td><td>Access by index is slower<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>34. What is the Difference Between HashMap and Hashtable?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>HashMap<\/strong><\/td><td><strong>Hashtable<\/strong><\/td><\/tr><tr><td>Not synchronized<\/td><td>Synchronized<\/td><\/tr><tr><td>Faster in single-threaded use<\/td><td>Slower due to synchronization<\/td><\/tr><tr><td>Allows one null key<\/td><td>Does not allow null key<\/td><\/tr><tr><td>Modern and commonly used<\/td><td>Legacy class<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>35. How Does HashMap Work Internally in Java?<\/strong><\/h3>\n\n\n\n<p>HashMap stores data in key-value pairs. It uses the key\u2019s hashCode() to decide where the entry should be stored.<\/p>\n\n\n\n<p>Each storage location is called a bucket. Java compares keys using .equals() when two keys have the same hash bucket. This process helps HashMap retrieve values quickly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>36. What is the Difference Between Comparable and Comparator?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Comparable<\/strong><\/td><td><strong>Comparator<\/strong><\/td><\/tr><tr><td>Defines natural sorting<\/td><td>Defines custom sorting<\/td><\/tr><tr><td>Uses compareTo() method<\/td><td>Uses compare() method<\/td><\/tr><tr><td>Implemented inside the class<\/td><td>Created outside the class<\/td><\/tr><tr><td>Example: sort by name by default<\/td><td>Example: sort by salary or age<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>37. What is an Iterator in Java?<\/strong><\/h3>\n\n\n\n<p>An <strong>Iterator<\/strong> is used to traverse elements in a collection one by one.<\/p>\n\n\n\n<p>It provides methods like hasNext(), next(), and remove(). Java developers use iterators when they need safe and controlled access to collection elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>38. What is the Difference Between Iterator and ListIterator?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Iterator<\/strong><\/td><td><strong>ListIterator<\/strong><\/td><\/tr><tr><td>Works with many collections<\/td><td>Works only with List<\/td><\/tr><tr><td>Moves forward only<\/td><td>Moves forward and backward<\/td><\/tr><tr><td>Can remove elements<\/td><td>Can add, remove, and update elements<\/td><\/tr><tr><td>Simple traversal<\/td><td>More flexible traversal<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>39. What is a Fail-Fast Iterator in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>fail-fast iterator<\/strong> throws ConcurrentModificationException when a collection is modified while being iterated.<\/p>\n\n\n\n<p>It helps detect unsafe changes during iteration. Common examples include iterators of ArrayList, HashMap, and HashSet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>40. What is a Fail-Safe Iterator in Java?<\/strong><\/h3>\n\n\n\n<p>A fail-safe iterator works on a copy of the collection instead of the original collection.<\/p>\n\n\n\n<p>It does not throw ConcurrentModificationException during iteration. Examples include iterators from CopyOnWriteArrayList and ConcurrentHashMap.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>41. What is Exception Handling in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/java-tutorial\/introduction-to-exceptions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Exception handling in Java<\/a> is a mechanism used to handle runtime errors without stopping the program suddenly.<\/p>\n\n\n\n<p>It uses try, catch, finally, throw, and throws. Good exception handling makes Java applications more stable and user-friendly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>42. What is the Difference Between Checked and Unchecked Exceptions?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Checked Exception<\/strong><\/td><td><strong>Unchecked Exception<\/strong><\/td><\/tr><tr><td>Checked at compile time<\/td><td>Checked at runtime<\/td><\/tr><tr><td>Must be handled or declared<\/td><td>Handling is optional<\/td><\/tr><tr><td>Example: IOException<\/td><td>Example: NullPointerException<\/td><\/tr><tr><td>Usually external issues<\/td><td>Usually programming mistakes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>43. What is the Difference Between throw and throws?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>throw<\/strong><\/td><td><strong>throws<\/strong><\/td><\/tr><tr><td>Used to throw an exception manually<\/td><td>Used to declare possible exceptions<\/td><\/tr><tr><td>Written inside a method<\/td><td>Written in method signature<\/td><\/tr><tr><td>Throws one exception object<\/td><td>Can declare multiple exceptions<\/td><\/tr><tr><td>Example: throw new Exception()<\/td><td>Example: throws IOException<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>44. What is the Purpose of the finally Block?<\/strong><\/h3>\n\n\n\n<p>The finally block contains code that should run after try and catch.<\/p>\n\n\n\n<p>It is commonly used to close files, database connections, network connections, or other resources. The finally block runs even when an exception occurs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>45. What is a Custom Exception in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>custom exception<\/strong> is a user-defined exception created for a specific application requirement.<\/p>\n\n\n\n<p>Developers create custom exceptions by extending Exception or RuntimeException. They are useful when business rules need clear error messages.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class InvalidAgeException extends Exception {\n\n&nbsp;&nbsp;&nbsp;&nbsp;InvalidAgeException(String message) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(message);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>46. What is the Difference Between Exception and Error?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Exception<\/strong><\/td><td><strong>Error<\/strong><\/td><\/tr><tr><td>Can usually be handled<\/td><td>Usually cannot be handled<\/td><\/tr><tr><td>Caused by application issues<\/td><td>Caused by serious system issues<\/td><\/tr><tr><td>Example: IOException<\/td><td>Example: OutOfMemoryError<\/td><\/tr><tr><td>Recovery is possible<\/td><td>Recovery is difficult<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>47. What is Multithreading in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/java-tutorial\/introduction-to-multithreading\/\" target=\"_blank\" rel=\"noreferrer noopener\">Multithreading in Java<\/a> allows multiple threads to run at the same time.<\/p>\n\n\n\n<p>It helps improve performance in tasks like file processing, background jobs, API calls, and server-side applications. Java supports multithreading through the Thread class and Runnable interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>48. What is the Difference Between Process and Thread?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Process<\/strong><\/td><td><strong>Thread<\/strong><\/td><\/tr><tr><td>Independent program in execution<\/td><td>Small unit inside a process<\/td><\/tr><tr><td>Has separate memory<\/td><td>Shares memory with other threads<\/td><\/tr><tr><td>Heavyweight<\/td><td>Lightweight<\/td><\/tr><tr><td>Slower communication<\/td><td>Faster communication<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>49. What is the Thread Lifecycle in Java?<\/strong><\/h3>\n\n\n\n<p>A Java thread moves through different states during execution.<\/p>\n\n\n\n<p>The main thread states are New, Runnable, Running, Waiting, Timed Waiting, Blocked, and Terminated. Understanding the thread lifecycle helps developers debug concurrency issues.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>50. What is Synchronization in Java?<\/strong><\/h3>\n\n\n\n<p>Synchronization controls access to shared resources in a multithreaded program.<\/p>\n\n\n\n<p>It prevents multiple threads from changing the same data at the same time. Java supports synchronization using the synchronized keyword.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>51. What is the Difference Between wait() and sleep() in Java?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>wait()<\/strong><\/td><td><strong>sleep()<\/strong><\/td><\/tr><tr><td>Releases the lock<\/td><td>Does not release the lock<\/td><\/tr><tr><td>Used for thread communication<\/td><td>Used to pause execution<\/td><\/tr><tr><td>Called on an object<\/td><td>Called on Thread<\/td><\/tr><tr><td>Must be inside synchronized block<\/td><td>No synchronized block required<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>52. What is Deadlock in Java?<\/strong><\/h3>\n\n\n\n<p>A <a href=\"https:\/\/www.guvi.in\/hub\/operating-system-tutorial\/deadlocks\/\" target=\"_blank\" rel=\"noreferrer noopener\">deadlock<\/a> in Java occurs when two or more threads are blocked forever because each thread is waiting for a resource locked by another thread.<\/p>\n\n\n\n<p>It usually happens in multithreaded programs when one thread holds Lock A and waits for Lock B, while another thread holds Lock B and waits for Lock A. Since neither thread releases its current lock, both remain stuck permanently.<\/p>\n\n\n\n<p>Deadlock can stop critical parts of a Java application, especially in systems that use synchronization, shared resources, database connections, or file locks. It can be prevented by acquiring locks in a fixed order, avoiding nested locks where possible, using lock timeouts, and keeping synchronized blocks as small as possible.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>53. How Can Deadlock Be Prevented in Java?<\/strong><\/h3>\n\n\n\n<p>Deadlock can be prevented by using a fixed lock order, avoiding nested locks, using timeout-based locking, and keeping synchronized blocks small.<\/p>\n\n\n\n<p>Good thread design also helps reduce deadlock risk in enterprise Java applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>54. What is the Difference Between Runnable and Callable?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Runnable<\/strong><\/td><td><strong>Callable<\/strong><\/td><\/tr><tr><td>Does not return a result<\/td><td>Returns a result<\/td><\/tr><tr><td>Cannot throw checked exceptions directly<\/td><td>Can throw checked exceptions<\/td><\/tr><tr><td>Uses run() method<\/td><td>Uses call() method<\/td><\/tr><tr><td>Used with Thread or Executor<\/td><td>Used with ExecutorService<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>55. What is ExecutorService in Java?<\/strong><\/h3>\n\n\n\n<p>ExecutorService is a Java concurrency framework used to manage thread execution.<\/p>\n\n\n\n<p>It helps create thread pools, submit tasks, control background jobs, and manage thread lifecycle. It is better than creating many threads manually.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>56. What is Garbage Collection in Java?<\/strong><\/h3>\n\n\n\n<p>Garbage collection in Java is the process of removing unused objects from memory automatically.<\/p>\n\n\n\n<p>The JVM identifies objects that are no longer referenced and clears them from heap memory. This helps reduce memory leaks and improves application stability.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>57. What is the Difference Between Stack and Heap Memory in Java?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Stack Memory<\/strong><\/td><td><strong>Heap Memory<\/strong><\/td><\/tr><tr><td>Stores method calls and local variables<\/td><td>Stores objects<\/td><\/tr><tr><td>Smaller in size<\/td><td>Larger in size<\/td><\/tr><tr><td>Faster access<\/td><td>Slower than stack<\/td><\/tr><tr><td>Memory is cleared after method execution<\/td><td>Managed by garbage collector<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>58. What is the final Keyword in Java?<\/strong><\/h3>\n\n\n\n<p>The final keyword in Java is used to apply restrictions on variables, methods, and classes. It helps protect values, method behavior, and class design from unwanted modification.<\/p>\n\n\n\n<p>A final variable can be assigned only once. After initialization, its value cannot be changed, which makes it useful for constants and fixed configuration values.<\/p>\n\n\n\n<p>A final method cannot be overridden by a subclass. This is useful when a parent class method contains important logic that should remain unchanged in the inheritance hierarchy.<\/p>\n\n\n\n<p>A final class cannot be extended by another class. This prevents inheritance and is commonly used when a class must remain secure, stable, or immutable, such as Java\u2019s String class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>59. What is the Difference Between final, finally, and finalize?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>final<\/strong><\/td><td><strong>finally<\/strong><\/td><\/tr><tr><td>Keyword<\/td><td>Block<\/td><\/tr><tr><td>Restricts changes<\/td><td>Runs cleanup code<\/td><\/tr><tr><td>Used with variables, methods, classes<\/td><td>Used with exception handling<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>60. What is String Immutability in Java?<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/hub\/java-examples-tutorial\/immutable-strings\/\" target=\"_blank\" rel=\"noreferrer noopener\">String immutability<\/a> means a String object cannot be modified after it is created. Once a string value is stored in memory, its internal character sequence remains unchanged.<\/p>\n\n\n\n<p>When an operation like concatenation, replacement, or trimming is performed, Java does not update the existing Stringobject. Instead, it creates a new String object with the modified value, while the original object remains the same.<\/p>\n\n\n\n<p>This design makes String safe for use in the String Pool, improves memory optimization through reuse, protects sensitive data such as class names and file paths, and supports thread safety because multiple threads can share the same Stringobject without synchronization issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Advanced-Level Java Developer Interview Questions and Answers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>61. What is the Java Memory Model?<\/strong><\/h3>\n\n\n\n<p>The Java Memory Model defines how Java threads interact with memory. It explains how variables are read, written, cached, and made visible across threads.<\/p>\n\n\n\n<p>Advanced Java developers use it to understand visibility, ordering, and safe communication between threads.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SharedData {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private boolean running = true;\n\n&nbsp;&nbsp;&nbsp;&nbsp;void stop() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;running = false;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;void runTask() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (running) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ may not see updated value immediately\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Without proper visibility control, one thread may not see changes made by another thread.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>62. What is the <\/strong><strong>volatile<\/strong><strong> Keyword in Java?<\/strong><\/h3>\n\n\n\n<p>The volatile keyword tells the JVM that a variable must always be read from main memory.<\/p>\n\n\n\n<p>It is useful when one thread updates a value and another thread needs to see the latest value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class TaskRunner {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private volatile boolean running = true;\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void stop() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;running = false;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public void run() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (running) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Running...\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>volatile improves visibility, but it does not make compound operations fully atomic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>63. What is the Difference Between <\/strong><strong>synchronized<\/strong><strong> and <\/strong><strong>ReentrantLock<\/strong><strong>?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>synchronized<\/strong><\/td><td><strong>ReentrantLock<\/strong><\/td><\/tr><tr><td>Built-in Java keyword<\/td><td>Lock class from concurrency package<\/td><\/tr><tr><td>Lock release is automatic<\/td><td>Lock release must be manual<\/td><\/tr><tr><td>Less flexible<\/td><td>More control over locking<\/td><\/tr><tr><td>No timeout support<\/td><td>Supports timeout and fairness<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.locks.ReentrantLock;\n\nclass Counter {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private final ReentrantLock lock = new ReentrantLock();\n\n&nbsp;&nbsp;&nbsp;&nbsp;private int count = 0;\n\n&nbsp;&nbsp;&nbsp;&nbsp;void increment() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lock.lock();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} finally {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lock.unlock();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>ReentrantLock is useful when developers need advanced lock control.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>64. What is <\/strong><strong>ThreadLocal<\/strong><strong> in Java?<\/strong><\/h3>\n\n\n\n<p>ThreadLocal gives each thread its own separate copy of a variable.<\/p>\n\n\n\n<p>It is often used for request context, user sessions, transaction IDs, and security context in backend Java applications.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserContext {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private static final ThreadLocal&lt;String&gt; userId = new ThreadLocal&lt;&gt;();\n\n&nbsp;&nbsp;&nbsp;&nbsp;static void setUserId(String id) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userId.set(id);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;static String getUserId() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return userId.get();\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;static void clear() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;userId.remove();\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>remove() is important because thread pools reuse threads.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>65. What is <\/strong><strong>CompletableFuture<\/strong><strong> in Java?<\/strong><\/h3>\n\n\n\n<p>CompletableFuture is used for asynchronous programming in Java.<\/p>\n\n\n\n<p>It allows tasks to run in the background and lets developers combine, transform, or handle results later.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.CompletableFuture;\n\npublic class Demo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompletableFuture&lt;String&gt; future =\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CompletableFuture.supplyAsync(() -&gt; \"Java\")\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.thenApply(result -&gt; result + \" Developer\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(future.join());\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>It is useful for API calls, background jobs, and non-blocking workflows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>66. What is the Fork\/Join Framework in Java?<\/strong><\/h3>\n\n\n\n<p>The <strong>Fork\/Join Framework<\/strong> splits a large task into smaller subtasks and executes them in parallel.<\/p>\n\n\n\n<p>It is useful for CPU-heavy operations where the task can be divided recursively.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.concurrent.RecursiveTask;\n\nclass SumTask extends RecursiveTask&lt;Integer&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private final int&#91;] numbers;\n\n&nbsp;&nbsp;&nbsp;&nbsp;private final int start, end;\n\n&nbsp;&nbsp;&nbsp;&nbsp;SumTask(int&#91;] numbers, int start, int end) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.numbers = numbers;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.start = start;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.end = end;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;protected Integer compute() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (end - start &lt;= 2) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int sum = 0;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = start; i &lt; end; i++) sum += numbers&#91;i];\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sum;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int mid = (start + end) \/ 2;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SumTask left = new SumTask(numbers, start, mid);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SumTask right = new SumTask(numbers, mid, end);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left.fork();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return right.compute() + left.join();\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>It follows the divide-and-conquer approach.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>67. What is Reflection in Java?<\/strong><\/h3>\n\n\n\n<p>Reflection allows Java programs to inspect classes, methods, fields, and constructors at runtime.<\/p>\n\n\n\n<p>Frameworks like Spring and Hibernate use reflection internally.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Employee {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private String name;\n\n}\n\npublic class ReflectionDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class&lt;?&gt; clazz = Employee.class;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(clazz.getName());\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(clazz.getDeclaredFields()&#91;0].getName());\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Reflection is powerful, but it can affect performance and security.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>68. What are Java Annotations?<\/strong><\/h3>\n\n\n\n<p>Annotations provide metadata about code.<\/p>\n\n\n\n<p>They are widely used in frameworks, testing tools, ORM tools, and dependency injection systems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@interface Audit {\n\n&nbsp;&nbsp;&nbsp;&nbsp;String value();\n\n}\n\n@Audit(\"User login tracking\")\n\nclass LoginService {\n\n}<\/code><\/pre>\n\n\n\n<p>Annotations do not directly execute logic. Tools and frameworks read them to decide behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>69. What is a Custom Annotation in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>custom annotation<\/strong> is a developer-defined annotation used to attach metadata to classes, methods, or fields.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.lang.annotation.*;\n\n@Retention(RetentionPolicy.RUNTIME)\n\n@Target(ElementType.METHOD)\n\n@interface LogExecutionTime {\n\n}\n\nclass PaymentService {\n\n&nbsp;&nbsp;&nbsp;&nbsp;@LogExecutionTime\n\n&nbsp;&nbsp;&nbsp;&nbsp;void processPayment() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Payment processed\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Custom annotations are useful in logging, validation, authorization, and framework-level programming.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>70. What is Serialization in Java?<\/strong><\/h3>\n\n\n\n<p>Serialization converts an object into a byte stream.<\/p>\n\n\n\n<p>The byte stream can be stored in a file, sent over a network, or transferred between services.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.Serializable;\n\nclass Employee implements Serializable {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private String name;\n\n&nbsp;&nbsp;&nbsp;&nbsp;private int age;\n\n&nbsp;&nbsp;&nbsp;&nbsp;Employee(String name, int age) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.age = age;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Deserialization converts the byte stream back into an object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>71. What is <\/strong><strong>serialVersionUID<\/strong><strong> in Java?<\/strong><\/h3>\n\n\n\n<p>serialVersionUID is a unique version ID used during serialization and deserialization.<\/p>\n\n\n\n<p>It checks whether the serialized object and class definition are compatible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.Serializable;\n\nclass Customer implements Serializable {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private static final long serialVersionUID = 1L;\n\n&nbsp;&nbsp;&nbsp;&nbsp;private String name;\n\n}<\/code><\/pre>\n\n\n\n<p>A mismatch can cause InvalidClassException.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>72. What is a ClassLoader in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>ClassLoader<\/strong> loads Java classes into JVM memory at runtime.<\/p>\n\n\n\n<p>Java uses class loaders for application classes, system classes, libraries, plugins, and application server environments.<\/p>\n\n\n\n<p>public class ClassLoaderDemo {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ClassLoader loader = ClassLoaderDemo.class.getClassLoader();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(loader);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>ClassLoader knowledge is useful in debugging classpath and deployment issues.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>73. What is the Difference Between <\/strong><strong>ClassNotFoundException<\/strong><strong> and <\/strong><strong>NoClassDefFoundError<\/strong><strong>?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>ClassNotFoundException<\/strong><\/td><td><strong>NoClassDefFoundError<\/strong><\/td><\/tr><tr><td>Checked exception<\/td><td>Error<\/td><\/tr><tr><td>Happens during dynamic class loading<\/td><td>Happens when class was present earlier but missing at runtime<\/td><\/tr><tr><td>Often caused by Class.forName()<\/td><td>Often caused by classpath or dependency issues<\/td><\/tr><tr><td>Can be handled<\/td><td>Usually indicates deployment failure<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>These errors are common in enterprise Java applications with complex dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>74. What is JIT Compilation in Java?<\/strong><\/h3>\n\n\n\n<p><strong>JIT compilation<\/strong> means Just-In-Time compilation.<\/p>\n\n\n\n<p>The JVM uses JIT to convert frequently executed bytecode into native machine code during runtime.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class HotCode {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 0; i &lt; 1_000_000; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;calculate(i);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;static int calculate(int num) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return num * num;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Repeatedly used code can become faster after JVM optimization.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>75. What is Escape Analysis in Java?<\/strong><\/h3>\n\n\n\n<p>Escape analysis is a JVM optimization technique.<\/p>\n\n\n\n<p>It checks whether an object is used only inside a method or escapes outside that method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class EscapeDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;int calculate() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Point p = new Point(10, 20);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return p.x + p.y;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\nclass Point {\n\n&nbsp;&nbsp;&nbsp;&nbsp;int x, y;\n\n&nbsp;&nbsp;&nbsp;&nbsp;Point(int x, int y) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.x = x;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.y = y;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>The JVM may optimize memory allocation when an object does not escape.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>76. What is Metaspace in Java?<\/strong><\/h3>\n\n\n\n<p><strong>Metaspace<\/strong> stores class metadata in Java.<\/p>\n\n\n\n<p>It replaced PermGen from Java 8 onward. Metaspace grows dynamically, but excessive class loading can still create memory problems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MetaspaceDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(String.class.getClassLoader());\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(MetaspaceDemo.class.getClassLoader());\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Metaspace issues often appear in applications that load many classes dynamically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>77. What is the Difference Between Shallow Copy and Deep Copy?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Shallow Copy<\/strong><\/td><td><strong>Deep Copy<\/strong><\/td><\/tr><tr><td>Copies object references<\/td><td>Copies actual nested objects<\/td><\/tr><tr><td>Shared nested objects remain linked<\/td><td>Nested objects become independent<\/td><\/tr><tr><td>Faster<\/td><td>More memory-intensive<\/td><\/tr><tr><td>Risky for mutable objects<\/td><td>Safer for complex objects<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Address {\n\n&nbsp;&nbsp;&nbsp;&nbsp;String city;\n\n}\n\nclass User {\n\n&nbsp;&nbsp;&nbsp;&nbsp;String name;\n\n&nbsp;&nbsp;&nbsp;&nbsp;Address address;\n\n}<\/code><\/pre>\n\n\n\n<p>A shallow copy may share the same Address object between two users.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>78. What are Generics in Java?<\/strong><\/h3>\n\n\n\n<p>Generics allow developers to write reusable and type-safe code.<\/p>\n\n\n\n<p>They reduce the need for manual casting and help catch type errors during compilation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Box&lt;T&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;private T value;\n\n&nbsp;&nbsp;&nbsp;&nbsp;void setValue(T value) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.value = value;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;T getValue() {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Box&lt;String&gt; box = new Box&lt;&gt;();\n\nbox.setValue(\"Java\");<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>79. What is Type Erasure in Java?<\/strong><\/h3>\n\n\n\n<p>Type erasure means generic type information is removed during compilation.<\/p>\n\n\n\n<p>Java uses generics for compile-time safety, but most generic details are not available at runtime.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class TypeErasureDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt; names = new ArrayList&lt;&gt;();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;Integer&gt; numbers = new ArrayList&lt;&gt;();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(names.getClass() == numbers.getClass());\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>This prints true because both become ArrayList at runtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>80. What is a Bounded Type Parameter in Java?<\/strong><\/h3>\n\n\n\n<p>A bounded type parameter restricts the type that can be used with generics.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Calculator&lt;T extends Number&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;double square(T value) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return value.doubleValue() * value.doubleValue();\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Here, T can be Integer, Double, Float, or another subclass of Number.<\/p>\n\n\n\n<p>Bounded types improve type safety in reusable Java code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>81. What is PECS in Java Generics?<\/strong><\/h3>\n\n\n\n<p>PECS stands for Producer Extends, Consumer Super.<\/p>\n\n\n\n<p>It helps developers decide when to use extends and super with wildcards.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\nclass Demo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;static double sum(List&lt;? extends Number&gt; numbers) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double total = 0;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (Number n : numbers) total += n.doubleValue();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return total;\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;static void addNumbers(List&lt;? super Integer&gt; numbers) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers.add(10);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numbers.add(20);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Use extends when reading values. Use super when adding values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>82. What is a Spliterator in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>Spliterator<\/strong> is used to traverse and split data for parallel processing.<\/p>\n\n\n\n<p>It is more advanced than a normal iterator because it can divide data into parts.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class SpliteratorDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt; names = List.of(\"Amit\", \"Riya\", \"John\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Spliterator&lt;String&gt; spliterator = names.spliterator();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;spliterator.forEachRemaining(System.out::println);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Spliterators are used internally by Java streams.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>83. What is a Method Handle in Java?<\/strong><\/h3>\n\n\n\n<p>A <strong>MethodHandle<\/strong> is a typed, directly executable reference to a method, constructor, or field.<\/p>\n\n\n\n<p>It is more flexible and often faster than reflection for repeated method calls.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.lang.invoke.*;\n\npublic class MethodHandleDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) throws Throwable {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MethodHandles.Lookup lookup = MethodHandles.lookup();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MethodHandle handle = lookup.findStatic(\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Math.class,\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"max\",\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MethodType.methodType(int.class, int.class, int.class)\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int result = (int) handle.invoke(10, 20);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(result);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Method handles are used in advanced JVM and framework-level programming.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>84. What is the Difference Between Reflection and MethodHandle?<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Reflection<\/strong><\/td><td><strong>MethodHandle<\/strong><\/td><\/tr><tr><td>Older runtime inspection API<\/td><td>Newer method invocation API<\/td><\/tr><tr><td>More general<\/td><td>More direct and typed<\/td><\/tr><tr><td>Often slower for repeated calls<\/td><td>Usually better for repeated calls<\/td><\/tr><tr><td>Common in frameworks<\/td><td>Common in JVM-level operations<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Reflection is easier to understand. MethodHandle is better for advanced dynamic invocation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>85. What are Records in Java?<\/strong><\/h3>\n\n\n\n<p>Records are special Java classes used to create immutable data carrier objects.<\/p>\n\n\n\n<p>They reduce boilerplate code by automatically generating constructor, accessor methods, equals(), hashCode(), and toString().<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>record Employee(String name, int age) {\n\n}\n\npublic class RecordDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Employee employee = new Employee(\"Riya\", 25);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(employee.name());\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(employee);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Records are useful for DTOs, <a href=\"https:\/\/www.guvi.in\/blog\/api-response-structure-best-practices\/\" target=\"_blank\" rel=\"noreferrer noopener\">API responses<\/a>, and read-only data models.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>86. What are Sealed Classes in Java?<\/strong><\/h3>\n\n\n\n<p>Sealed classes restrict which classes can extend or implement them.<\/p>\n\n\n\n<p>They help developers control inheritance in complex domain models.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed class Payment permits CardPayment, UpiPayment {\n\n}\n\nfinal class CardPayment extends Payment {\n\n}\n\nfinal class UpiPayment extends Payment {\n\n}<\/code><\/pre>\n\n\n\n<p>Sealed classes make code more predictable and safer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>87. What is Pattern Matching in Java?<\/strong><\/h3>\n\n\n\n<p>Pattern matching simplifies conditional logic by combining type checking and variable extraction.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PatternDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;static void printLength(Object value) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (value instanceof String text) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(text.length());\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>It avoids unnecessary casting and makes code cleaner.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>88. What is the Java Module System?<\/strong><\/h3>\n\n\n\n<p>The Java Module System helps organize large applications into modules.<\/p>\n\n\n\n<p>It was introduced in Java 9 to improve dependency control and application structure.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module com.example.app {\n\n&nbsp;&nbsp;&nbsp;&nbsp;requires java.sql;\n\n&nbsp;&nbsp;&nbsp;&nbsp;exports com.example.service;\n\n}<\/code><\/pre>\n\n\n\n<p>Modules make Java applications easier to maintain at scale.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>89. What is the Foreign Function and Memory API in Java?<\/strong><\/h3>\n\n\n\n<p>The Foreign Function and Memory <a href=\"https:\/\/www.guvi.in\/hub\/network-programming-with-python\/understanding-apis\/\" target=\"_blank\" rel=\"noreferrer noopener\">API<\/a> allows Java programs to interact with native code and memory outside the JVM.<\/p>\n\n\n\n<p>It is designed as a safer modern alternative to JNI for many use cases.<\/p>\n\n\n\n<p>\/\/ Conceptual example<\/p>\n\n\n\n<p>\/\/ Java can call native libraries and access off-heap memory<\/p>\n\n\n\n<p>\/\/ using the Foreign Function and Memory API.<\/p>\n\n\n\n<p>This is useful in high-performance systems, native integrations, and low-level libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>90. What are Virtual Threads in Java?<\/strong><\/h3>\n\n\n\n<p>Virtual threads are lightweight threads designed for high-concurrency applications.<\/p>\n\n\n\n<p>They make it easier to handle many blocking tasks without creating thousands of expensive platform threads.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class VirtualThreadDemo {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) throws InterruptedException {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread thread = Thread.startVirtualThread(() -&gt; {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(\"Running in virtual thread\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;thread.join();\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}<\/code><\/pre>\n\n\n\n<p>Virtual threads are useful for scalable server-side Java applications, especially when handling many requests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Scenario-Based Java Developer Interview Questions and Answers<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>91. A Spring Boot API is taking too long to respond. How would you debug it?<\/strong><\/h3>\n\n\n\n<p>I would first check logs, response time, database queries, external API calls, and server resource usage.<\/p>\n\n\n\n<p>Slow Java APIs often happen because of unoptimized SQL queries, missing indexes, large payloads, slow third-party services, or blocking operations. I would use tools like application logs, <a href=\"https:\/\/www.guvi.in\/blog\/what-is-spring-boot-ai\/\" target=\"_blank\" rel=\"noreferrer noopener\">Spring Boot<\/a> Actuator, APM tools, and database query analysis to find the exact bottleneck.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>92. A user clicks the payment button twice. How would you prevent duplicate payment processing?<\/strong><\/h3>\n\n\n\n<p>I would use idempotency.<\/p>\n\n\n\n<p>Each payment request should have a unique idempotency key. The backend should check whether the same key was already processed before creating a new payment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (paymentRepository.existsByRequestId(requestId)) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;return existingPaymentResponse;\n\n}\n\nprocessPayment(request);<\/code><\/pre>\n\n\n\n<p>This prevents duplicate transactions in real-world Java backend systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>93. A <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/database-management-guide-with-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>database<\/strong><\/a><strong> query works fine in testing but becomes slow in production. What would you check?<\/strong><\/h3>\n\n\n\n<p>I would check data volume, indexes, query execution plan, joins, pagination, and database locks.<\/p>\n\n\n\n<p>A query may work fast with 100 records but become slow with 10 lakh records. I would also check whether the Java application is fetching unnecessary columns or loading too many related entities.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>94. Your Spring Boot service is returning too much data in one API response. What would you do?<\/strong><\/h3>\n\n\n\n<p>I would add pagination, filtering, and response DTOs.<\/p>\n\n\n\n<p>Large API responses increase memory usage and slow down both backend and frontend performance.<\/p>\n\n\n\n<p>Page&lt;User&gt; users = userRepository.findAll(PageRequest.of(0, 20));<\/p>\n\n\n\n<p>A clean Java API should return only the required data for the current screen or operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>95. A Java application frequently runs out of database connections. How would you solve it?<\/strong><\/h3>\n\n\n\n<p>I would check whether connections are being closed properly and review the connection pool configuration.<\/p>\n\n\n\n<p>In Spring Boot, I would inspect HikariCP settings, long-running queries, transaction boundaries, and connection leaks. Poor connection handling can slow down the full application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>96. A <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/what-is-rest-api\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>REST API<\/strong><\/a><strong> sometimes saves partial data when an error occurs. How would you fix it?<\/strong><\/h3>\n\n\n\n<p>I would use proper <strong>transaction management<\/strong>.<\/p>\n\n\n\n<p>In Spring Boot, @Transactional ensures that all database operations inside a method succeed together. The transaction rolls back when an error occurs.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Transactional\n\npublic void placeOrder(OrderRequest request) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;saveOrder(request);\n\n&nbsp;&nbsp;&nbsp;&nbsp;updateInventory(request);\n\n&nbsp;&nbsp;&nbsp;&nbsp;savePayment(request);\n\n}<\/code><\/pre>\n\n\n\n<p>This keeps data consistent in Java enterprise applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>97. A Java service depends on another service that often fails. How would you protect your application?<\/strong><\/h3>\n\n\n\n<p>I would use timeout handling, retries, fallback logic, and a circuit breaker.<\/p>\n\n\n\n<p>A Java service should not keep waiting forever for a failing external service. Tools like Resilience4j can help prevent cascading failures in microservice-based systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>98. A login API is working, but users can access protected APIs without proper validation. What would you check?<\/strong><\/h3>\n\n\n\n<p>I would check authentication, authorization, token validation, role checks, and security filters.<\/p>\n\n\n\n<p>A secure Java backend should verify every protected request. JWT expiry, signature validation, user roles, and endpoint permissions must be handled correctly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>99. Your application logs are full of errors, but they do not explain the actual issue. What would you improve?<\/strong><\/h3>\n\n\n\n<p>I would improve structured logging, exception messages, request IDs, and log levels.<\/p>\n\n\n\n<p>Good Java application logs should show what failed, where it failed, and which request caused it. I would avoid logging sensitive data such as passwords, tokens, and payment details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>100. A frequently used API is putting heavy load on the database. How would you optimize it?<\/strong><\/h3>\n\n\n\n<p>I would use caching where the data does not change often.<\/p>\n\n\n\n<p>For example, product categories, country lists, configuration data, and user permissions can be cached.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Cacheable(\"products\")\n\npublic Product getProductById(Long id) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;return productRepository.findById(id).orElseThrow();\n\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/caching-in-system-design\/\" target=\"_blank\" rel=\"noreferrer noopener\">Caching<\/a> reduces repeated database calls and improves Java application performance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Java Interview Tips<\/strong><\/h2>\n\n\n\n<ul>\n<li><strong>Start with the Core Definition: <\/strong>Give a clean one-line definition first. Then explain where the concept is used in Java applications.<\/li>\n\n\n\n<li><strong>Add One Practical Example: <\/strong>Connect every answer to backend systems, APIs, databases, threads, memory, or Spring Boot use cases.<\/li>\n\n\n\n<li><strong>Do Not Stop at Syntax: <\/strong>Interviewers want to know why a concept matters. Explain the purpose, behavior, and real-world impact.<\/li>\n\n\n\n<li><strong>Compare Similar Concepts Clearly:<\/strong><strong><br><\/strong>Prepare differences like ArrayList vs LinkedList, throw vs throws, wait vs sleep, and final vs finally.<\/li>\n\n\n\n<li><strong>Explain JVM Topics Slowly: <\/strong>For memory, garbage collection, JIT, metaspace, and class loading, use simple flow-based explanations.<\/li>\n\n\n\n<li><strong>Prepare Concurrency Carefully: <\/strong>Multithreading questions are common. Be ready with synchronization, deadlock, volatile, ThreadLocal, and ExecutorService.<\/li>\n\n\n\n<li><strong>Think Like a <\/strong><a href=\"https:\/\/www.guvi.in\/blog\/what-is-backend-development\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Backend Developer<\/strong><\/a><strong>: <\/strong>For scenario questions, mention logs, database queries, API response time, connection pools, transactions, caching, and security checks.<\/li>\n\n\n\n<li><strong>End Answers with Confidence: <\/strong>Close with a practical takeaway. Example: \u201cThis improves performance and keeps the application stable in production.\u201d<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/java-interview-questions-and-answers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java interviews<\/a> can feel challenging because they test both fundamentals and real-world development thinking. A strong answer is not only about defining a concept correctly. It is also about explaining how that concept works in backend systems, APIs, databases, memory management, and production environments.<\/p>\n\n\n\n<p>This list of Top 100 Java Developer Interview Questions and Answers gives you a complete revision path from beginner-level basics to advanced JVM concepts and scenario-based problem-solving. Practice the code examples, understand the logic behind each answer, and explain every concept in your own words. That is what helps you sound confident, practical, and interview-ready.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick Answer: This guide covers 100 Java developer interview questions across beginner, intermediate, advanced, and scenario-based levels. It helps you revise Java fundamentals, OOP, collections, multithreading, JVM concepts, Spring Boot use cases, and backend problem-solving with clear answers. Java was released by Sun Microsystems in 1995, but even after three decades, it still powers some [&hellip;]<\/p>\n","protected":false},"author":60,"featured_media":70904,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[720,294,719],"tags":[],"views":"6852","authorinfo":{"name":"Vaishali","url":"https:\/\/www.guvi.in\/blog\/author\/vaishali\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/Java-1-300x112.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70885"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/60"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=70885"}],"version-history":[{"count":13,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70885\/revisions"}],"predecessor-version":[{"id":114275,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70885\/revisions\/114275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/70904"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=70885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=70885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=70885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}