Java Interview Questions and Answers

Are you preparing for a Java developer interview in 2025? Whether you’re a beginner, a fresher, or an experienced professional, mastering Java interview questions is very important to crack your next technical Java interview.

In this comprehensive guide, we have researched and created a list of the top 100+ Java interview questions and answers that generally cover everything from OOP concepts, exception handling, collections framework, multithreading, generics, to features like streams and lambda expressions. It also covers essential concepts across newer versions like Java 21, Java 22, and Java 23, a valuable resource.

These Java Interview Questions are majorly asked in companies such as TCS, Infosys, Wipro, Capgemini, Cognizant, as well as product-based companies such as Amazon, Google, and Oracle.

Whether you are preparing for core Java roles or full-stack roles, this set of Java interview questions and answers covers everything of Java programming for freshers, mid-level, as well as for experienced developers, to help you prepare for your next Java technical interview preparation in 2025. These updated questions not only assist in cracking interviews, but also in establishing solid Java fundamentals that all Java programmers should have.

Table of Contents:

Basic Java Interview Questions

Lets start with basic Java Interview Questions and answers that generally covers important Java concepts that will help beginners and freshers to solidify their Java knowledge for the next Interview.

1. What is Java, and what makes it popular among developers?

Java is a high-level, object-oriented programming language developed by James Gosling,  vice-president at Sun Microsystems. Java stands out due to unique features like platform independence, automatic memory and pointer management, object-oriented design, built-in multithreading. These features have made Java a popular choice among developers and software companies worldwide.

2. What makes the Java platform independent?

Java’s ‘Write Once, Run Anywhere’ feature allows it to be platform-independent. Once a Java program is written, it can run on any machine or operating system with Java installed.

When the code is compiled, it is compiled into an intermediate language called Bytecode. Bytecode is not dependent on any operating system or hardware. Once the code is compiled into Bytecode, it is then executed by the Java Virtual Machine (JVM).

JVM is platform-dependent and needs to be installed on the platform where you want the code to run. Therefore, as long as the target platform has a JVM installed, it can run any code that is written in Java, making it platform-independent.

3. Explain the differences between Java and C++

Feature Java C++
Design Philosophy More focus on portability and simplicity. It is a software layer that provides the necessary environment to run Java programs. (Note: This point seems misplaced; see correction below)
Platform Independence Platform independent, just needs the target platform’s JVM installed. Consists of JVM, Java libraries like .util and .lang, and runtime tools like garbage collector. (Note: This also seems Java-specific; see correction below)
Memory Management Automatic Memory Management. Manual memory management.
Execution Speed It is slower since the code is first converted to bytecode and then interpreted/compiled by the JVM. Faster due to direct compilation to machine code.
Syntax Complexity Simpler Syntax. Complex Syntax compared to Java.
Application Domains Used in enterprise applications, web development, Android apps, etc. Used in system software, game development, embedded systems, etc.

4. What is the difference between JDK, JRE, and JVM in Java?

JDK JRE JVM
It includes the full suite of tools required for developing Java applications. It is a software layer that provides the necessary environment to run Java programs. JVM converts Java Code into Bytecode.
Includes JRE, JVM, Java compiler, java debugger, and javadoc. It consists of the JVM, java libraries like .util and .lang, and runtime tools like java collector. It is platform dependent i.e., it has different versions of Mac, Linux, and Windows.

5. Which features of the Java programming language make it popular?

Java’s popularity lies in the features it offers. Here are the top features provided by Java:

  • Platform Independent: Code can run on any platform.
  • Object Oriented: Follows the principles of OOPS.
  • Simple and Easy to Learn: Doesn’t include complex features such as pointers, operator overloading etc.
  • Secure: Explicit access to pointers is unavailable, making it less vulnerable.
  • Multi-threaded: Supports parallel computing.
  • Distributed Computing: Built-in support for remote method invocation and protocols like HTTP and FTP.
quiz-icon
Are you ready for your interview?
Take a quick Quiz to check it out

6. Explain the following syntax: public static void main(String args[]) in java?

Public static void main

Every Java program needs a main method. When you compile a Java program, the JVM looks for the main method with the exact same syntax. Let’s break down the syntax to understand it better.

Public: The access specifier ‘public’ helps JVM access the method from outside the class.

Static: The static keyword helps to make a method or variable belong to a class, and not to an instance of a class, which is its object. This is useful because even if an object is instantiated from the class, the main method will remain global and not instantiate again, hence not confusing the JVM.

Void: This is a return type, and means the main method is not returning any value to the caller. In this case, it’s the JVM.

main(String args[]): This is the main method, with parameters of args[]. args[] helps to catch any command line arguments passed while launching the program.

7. If the main method is not declared as static, what will happen?

The program will compile successfully, but it will produce the following runtime error when executed.

Error: Main method is not static in class Example, please define the main method as:

public static void main(String[] args)

8. What are the different kinds of data types in Java?

Java primarily has two kind of data types:

  1. Primitive Data Types: These are the most basic data types, they have simple values and are not objects. Example: byte, short, int, etc.
  2. Non-Primitive Data types: These are also called reference types and are used to store references to objects in memory. Example: strings, arrays, classes, etc.
Build Your Reputation as a Java Expert
Join the Best Java Training Program
quiz-icon

9. Why can’t we use pointers in Java?

Pointers allow direct access to memory, and is a key feature of C/C++, however Java’s design intentionally disallows direct access to memory and therefore it does not make use of pointers. This design decision is to align Java to be a safe, simple, platform independent and secure programming language.

But why can the use of pointers be problematic? Let’s understand:

  • Safety: Direct access to memory via pointers can lead to a lot of vulnerabilities such as unauthorized memory access, memory corruption, overflows etc. Java automatically manages the memory, hence no room for human error.
  • Simplicity: Since java has automatic memory management, it is more simple to use.
  • Platform Independent: Different systems have different memory layouts, Java abstracts the memory management, and manages it via JVM. Therefore the same code can run on any platform.
  • Secure: Restricted memory access and automatic memory management promotes better security.

10. What are Classes and Objects in Java?

A class is the blueprint or template for creating an object and an object is an instance of a class with all its properties.

Let’s take a real world example to understand this:

There are so many models of cars, for example Mercedes, BMW, Audi, etc. These models can be considered as objects. They have been derived from a blueprint which is a “car”.

Let’s consider cars as a class or blueprint.

A car has 4 wheels, it has an engine, it has brakes, wipers, headlights etc. These features of a car are called the properties of a car.

An object of this class ‘car’, will have all these properties. Cars like BMW, Mercedes and Audi, all have these properties. Hence, we call them objects of the class car.

Similarly in Java, we can define a class with custom functions, variables, constructors, etc. And whenever, we define an object which is derived from a class, it inherits all its properties such as variables, methods etc.

Here’s an Example code to better understand this.

Java

Here, obj is derived from class Sample. The moment obj is initialized, it will print “Hello from Sample Class!!.” This is because it derives the properties of the sample class.

Get 100% Hike!

Master Most in Demand Skills Now!

11. Explain the concept of Inheritance in Java.

Inheritance helps a child class acquire the properties and behaviour of its parent class. It’s similar to how kids inherit many traits from their parents, such as their appearance, behavior, etc.

Another real world example would be Vehicles. All vehicles inherently have the same properties such as tyres, engine etc. But each vehicle type has its own unique features.

Similarly in java, when you create a class, and want to inherit the properties of it in another class, we make use of inheritance. Inheritance offers code reusability.

Take a look at the code below:

Java

In the code, there is a parent class Vehicles. It has three functions, tyres(), engine() and doors(). We create a NewCar class which inherits the Vehicles class using the ‘extends’ keyword.

Now, NewCar will have all the properties of the Vehicle class which includes its methods.

quiz-icon
Are you ready for your interview?
Take a quick Quiz to check it out

12. What is the concept of method overloading in Java?

When a Java program contains more than one method with the same name but with different argument types, it is called method overloading.

Java resolves this problem by checking the types and number of arguments; based on these, it calls the appropriate method.

For Example

Java

When a function is called with a specific argument, the corresponding method is invoked. For example, if you pass the value 10, the method that accepts an integer will be called; if you pass a string, the method that accepts a string will be executed.

Watch this Java Interview Questions video:

Video Thumbnail

13. Explain the working of == operator in Java.

The “==” operator in Java is used to compare primitives, object references and more. Its behaviour varies depending on the context. Let’s explore the differences in the table below:

Context What == compares?
Primitives Values
Object References Memory Locations
String Literals Memory Locations in string pools
Wrapper Objects Memory locations (Cached or not)

14. What are the differences between abstract class and interface?

Below are the major differences between the abstract class and an interface:

Abstract Class Interface
Can provide complete, default, or only the details in method overriding. Doesn’t provide any code, the user can only have the signature of the interface.
Have instance variables. No instance variables.
An abstract class can be public, private, or protected. The interface must be public.
An abstract class can have constructors. No constructors are available in the interface.
A class can inherit only a single abstract class. A class can implement one or more interfaces.
Abstract classes are fast. Interfaces are slower as they have to locate the corresponding methods in the class.

15. Explain the try-catch block in Java.

A try-catch block in Java is used to implement exceptional handling. The code that may throw exceptions has to be written inside the try block, and if any exception occurs, it is caught and handled in the corresponding catch block.

16. Explain the meaning of the ‘final’ keyword in Java. Give an example.

Java uses the final keyword to declare constants, prevent method overriding, and create immutable classes.

For example, final int MAX_SIZE = 100; declares a constant MAX_SIZE whose value is 100.

17. Explain the use of the ‘super’ keyword in Java?

Super is used in the Java programming language to refer to the superclass of the current object. It can also be used as a bridge between superclass methods/constructors and subclass methods/constructors. For instance, super.methodName() invokes the method defined by the superclass.

18. List differences between Object-oriented and Object-based programming languages?

The following are the key differences between the object-oriented and object-based programming languages:

Object-oriented programming language Object-based programming language
Object-oriented programming languages support all the features of OOPs, including polymorphism and inheritance. Object-based programming language does not support inheritance or polymorphism.
Doesn’t support built-in objects. Support built-in objects like JavaScript has the window object.
Example: C#, C++, Java, Python, etc. JavaScript and Visual Basic are examples of object-based programming languages.

19. What is data encapsulation in java?

Encapsulation in Java is the process of bundling data and methods within a class while restricting direct access to the class’s internal state. The reason behind this approach is to ensure data integrity and promote abstraction by exposing only essential details of the class’s behavior through public methods.

20. What is a constructor in Java? Explain the different types.

In Java, a constructor is a special type of method used to initialize objects. Constructors are automatically called when an object of a class is created. They must have the same name as the class and do not have any return type, not even void – unlike regular methods.

There are two types of constructors in the Java:

  1. Parameterized Constructor: A constructor that accepts arguments or parameters is known as the parameterized constructor. It initializes the instance variables and the values passed while creating an object.

Below is an example of the parameterized constructor:

Java
  1. Non-parameterized/Default constructor: The constructors with no parameters or arguments are known and non-parameterized constructors. In case you don’t define a constructor of any type, the compiler itself creates a default constructor which will be called when the object is created.

Below is an example of a non-parameterized constructor:

Java

21. What is the significance of (String[] args) in Java?

In Java, the (String[] args) argument exists within the main() method, and this is the entry point of any Java program. The array of strings permits the program to take command-line arguments when the program is executed. Every argument that is passed via the command line is placed within the array as a String object.

For instance, when you execute a Java program like this:

java MyApp Hello World

Then args[0] would be “Hello” and args[1] would be “World”.

This is very helpful in enterprise applications, batch jobs, or scripting where parameters have to be passed at runtime. So, String[] args makes your program flexible and configurable without the need to hardcode values.

22. What is a Method in Java?

A method in Java is generally a block of code designed to perform a specific task and is executed only when called. These Java Methods help in writing modular, reusable, and maintainable code.

Methods in Java can be:

  • Predefined (like System.out.println())
  • User-defined (where developers create their own custom logic)

Each method has a:

  • Return type (like int, void, String, etc.)
  • Method name
  • Optional parameters
  • Body that contains the logic

For example:

public int add(int a, int b) {
    return a + b;
}

Using Java methods also promotes code reusability and separation of concerns, which are generally the key principles in object-oriented programming and clean architecture.

23. Explain the differences between method overriding and method overloading.

Feature Method Overriding Method Overloading
Definition Redefining a method in a subclass Defining multiple methods with same name but different parameters
Class Relationship Requires inheritance (subclass & superclass) Happens within the same class
Method Signature Must be the same (name, parameters) Must differ (parameters type, number, or order)
Runtime or Compile Time Resolved at runtime (dynamic binding) Resolved at compile time (static binding)
Access Modifiers Cannot reduce visibility No such restriction
Use Case To provide specific implementation in subclass To perform different tasks with same method name

Code Example:

Java

24. What is a package in Java? Explain built-in and user-defined packages.

A package in Java is a grouping of related classes and interfaces under a namespace. It is like a folder that prevents naming conflicts and enhances the maintainability of the code.

There are two kinds of packages in Java:

  • Built-in Packages: These are offered by the Java API. For instance:
    • util (for collections such as ArrayList, HashMap)
    • io (for input-output operations)
    • net (for networking features)
  • User-defined Packages: These packages are defined by developers to logically organize their own classes. For instance:
package com.myapp.utils;
public class Logger {
    // utility methods
}

Packages also have a fundamental function in control of access and are relied upon to construct modular Java applications.

25. What are the key components to run a Java program?

To run a Java program, you need:

  • JDK (Java Development Kit) – compiler and tools included.
  • JRE (Java Runtime Environment) – includes JVM + libraries.
  • Java compiler (javac) – compiles source code to bytecode.
  • JVM (Java Virtual Machine) – executes the bytecode on any operating system.

The components interact together to compile and run Java code.

26. What is the purpose of the break keyword in Java?

The break statement is mainly utilized to stop a loop or switch prematurely. When there is a break in a loop or a switch, the JVM stops executing the current control structure immediately, irrespective of the leftover iterations or conditions.

Common Use Cases:

  • Exiting a for loop, while loop, or do-while loop early, depending on a condition.
  • Ending a switch case to avoid fall-through.

Example – Using break in a loop:

Java

Example – Using break in a switch:

Java

27. What is the difference between System.out.print() and System.out.println() in Java?

The key difference between System.out.print() and System.out.println() lies in line termination:

  • out.print() prints text without appending a newline at its conclusion.
  • out.println() prints text and then advances the cursor to a new line.

Comparison Example:

System.out.print("Hello ");
System.out.print("World");
System.out.println("Hello");
System.out.println("World");

Use println() when you need outputs on different lines, and use print() when you want to keep going on the same line.

28. How do you declare a constant variable in Java?

In Java, constants are declared using the final keyword. This ensures the variable’s value cannot be modified after initialization.

Syntax:

final int MAX_USERS = 100;

To make a constant accessible without instantiating a class, use both static and final:

public static final double PI = 3.14159;

Best Practices:

  • Use UPPERCASE with underscores for naming constants.
  • Define constants within a separate Constants class or interface for reuse.

29. What is the purpose of the default case in a switch statement in Java?

The default case in a switch statement is used as a catch-all when none of the case values match an input expression. It is semantically equivalent to an else block in an if-else ladder.

Why it matters:

  • Handles invalid or unexpected inputs.
  • Prevents the switch block from doing nothing if no match is found.

Example:

Java

Although it is optional, having a default case is a best practice to maintain solid and defensive programming.

30. What is a loop? Name the types of loops in Java.

A loop in Java is a control structure through which Java code is repeated over and over again on the basis of a given condition. A loop minimizes code redundancy and is used in cases such as traversing arrays or collections.

Types of Loops in Java:

  1. For loop: Used when there is a predetermined count of iterations.
  2. While loop: Repeats as long as the condition is true; utilized when the number of iterations is unknown.
  3. Do-while loop: This loop repeats its body at least once before it checks for its condition.
  4. Enhanced for loop (for-each): Suitable for looping through arrays and collections.

31. How do you concatenate two strings in Java?

You can concatenate two or more strings in Java by:

1. The + Operator:

String fullName = "Ayaan" + " " + "Alam";

2. The concat() Method:

String fullName = "Ayaan".concat(" ").concat("Alam");

3. StringBuilder or StringBuffer (for better performance in loops):

StringBuilder sb = new StringBuilder();
sb.append("Ayaan").append(" ").append("Alam");
String fullName = sb.toString();

Best Practice: StringBuilder in high-performance situations such as extensive concatenations or within loop iterations

32. What is the difference between int and Integer in Java?

  • int is a primitive data type that stores a 32-bit signed integer.
  • Integer is a wrapper class for an int type and is a part of Java Collections Framework.

Key Differences:

Feature int Integer
Type Primitive Object (Wrapper class)
Nullability Cannot be null Can be null
Use in Collections Not allowed Allowed (e.g., in ArrayList<Integer>)
Methods No methods Provides utility methods (e.g., parseInt())

Example:

int a = 10;
Integer b = Integer.valueOf(a);  // Autoboxing

33. How do you create a single-line comment in Java?

A single line comment in Java is created using two forward slashes //. Any text following // on the same line is considered as a comment and ignored by the compiler.

Example:

// This is a single-line comment

 int x = 10; // This sets the value of x to 10

Use Case:

Single line comments exist to comment on brief lines of code or to comment out a line temporarily when debugging.

34. What is the ‘continue’ keyword used for in Java?

The continue keyword in Java is used inside loops to skip the current iteration and proceed directly to the next one. It is commonly used when a specific condition is met, and you want to skip the remaining statements in the current loop cycle without terminating the loop entirely.

Example:

Java

Use Case:

Improves code clarity by avoiding unnecessarily deeply-nested if statements when there is a need to skip logic within a loop.

35. What is the superclass of every class in Java?

The java.lang.Object class is the superclass of all classes in Java.

Even when a class does not explicitly extend another class , the Java compiler automatically makes it extend the Object class.

In this way, each class has methods such as:

  • toString()
  • equals(Object obj)
  • hashCode()
  • getClass()
  • notify(), wait(), clone() (if applicable)

The inheritance here guarantees uniformity and allows Java objects to be compatible with collections, reflection, and other language aspects.

36. How do you define an interface in Java?

In Java, the interface keyword is used to define an interface which serves as a blueprint containing abstract methods and constants.

Java

Interfaces can also include:

  • Default methods
  • Static methods
  • Private methods (since Java 9)

Interfaces are employed to facilitate abstraction, decouple components, and provide multi-inheritance by means of contract programming.

Intermediate Java Interview Questions

Now , with this section we will discuss Intermediate Java Interview Questions that will help Java developers with few years of experience who wants to go higher on their Java Development domain.

37. How does the JVM work?

Java Virtual Machine is a Java Runtime Environment (JRE) component. It loads, verifies, and executes the bytecode. Although JVM is platform-dependent, it enables Java code to be platform-independent.

38. Explain the working of JIT.

JIT (Just-in-Time) is a runtime component of the Java Virtual Machine (JVM) that converts bytecode into machine code. Its main purpose is to speed up code execution, thereby improving the performance and efficiency of Java applications.

It does so by recognizing code blocks that are executed frequently and converting them into machine code. This compiled machine code is then saved in memory and whenever the same code block needs to run again, JVM can use the already compiled version instead of recompiling it..

By avoiding repeated compilation of the same code, the JIT compiler improves execution speed and reduces the workload on the JVM.

39. Which memory areas are allocated by JVM?

JVM is the core component of the Java Platform, responsible for running Java programs. To execute the programs, JVM needs to store instructions, variables, and other data in memory.. JVM’s memory is divided into specific areas, each designed for a specific purpose. Let’s understand them:

  1. Method Area (Metaspace): This area of the memory saves the class metadata, method metadata, field metadata and static variable.
  2. Heap: This is the largest memory area, and it stores objects and their associated instance variables.
  3. Java Stacks(Thread stacks): It is a runtime memory area which is created for each thread. It stores local variables, method arguments, etc. It is designed to handle method execution and local variables.
  4. PC Registers: The program counter register helps point to the current thread being executed. After executing an instruction the PC points to the next instruction.
  5. Native Method Stacks: It is a memory area in JVM which is reserved for native methods written in languages other than Java. Like C/C++. It helps java interact with system level APIs which are not written in Java.

40. Why are wrapper classes needed in Java?

We need wrapper classes to convert primitive data types to objects. They are required for special use cases such as follows:

  1. Working with Collections: Collections in java work with objects and not primitive data types, so wrapper classes help convert primitive data types to collections.
  2. Converting Data Types: Wrapper classes can help convert Strings to primitive data types.
  3. Utility Methods: Wrapper classes have useful utility methods, which if we want to use, then we have to convert primitive data type to object.
  4. Autoboxing and Unboxing: Java can automatically convert between primitive data types and their corresponding wrapper objects whenever needed.

41. Explain the following variables in Java:

a. Instance Variables
b. Local Variables
c. Class Variables

Instance variables are declared inside the class but outside the methods. So basically these are accessible by all the methods of that class. The default value for numeric types is 0, for boolean false and null for object references.

Example:

Java

Local variables’ scope is limited to the function, constructor, or block of code, so no other function can access these variables. These variables don’t have any default value.

Example:

Java

Class variables are also known as static variables in Java, defined in a class using static keywords. These variables are stored in static memory and can be called in a program using the class name. The default values are the same as the instance variables.

Example:

Java

The main difference between Instance variables and Class Variables is that the instance variables belong to only a specified class in which they are declared. While the class variables belong to classes and subclasses.

42. Is Java a pure object-oriented programming language?

No, Java is not a pure object-oriented programming language because it uses primitive data types like char, boolean, int, short, long, double etc. and also supports the concepts of static methods and variables, which can be directly accessed with class names. A pure object oriented programming language treats everything as an object, however java uses both Object oriented programming and procedural programming language features.

43. What are the differences between StringBuffer and StringBuilder in Java programming?

StringBuffer StringBuilder
StringBuffer methods are synchronized. StringBuilder is non-synchronized.
StringBuffer is thread-safe. StringBuilder is not thread-safe.
The performance is very slow. The performance is very fast.

44. List the differences between an ArrayList and a Vector.

Feature ArrayList Vector
Synchronization An ArrayList is not synchronized. A vector is synchronized.
Performance An ArrayList is fast. A vector is slow as it is thread-safe.
Growth Policy If an element is inserted into an ArrayList, it increases its array size by 50 percent. A vector defaults to doubling the size of its array.
Capacity Increment An ArrayList does not define the increment size. A vector defines the increment size.
Traversal Mechanism An ArrayList uses an Iterator for traversing. Except for the hashtable, a vector is the only other class using Enumeration and Iterator.

45. Explain Iterator vs Enumeration.

Iterator Enumeration
Iterator is an interface found in the java.util package. Enumeration is a special data type that allows you to define a fixed set of constants.
Uses three methods to interface:
  1. hasNext()
  2. next()
  3. remove()
Uses two methods:
  1. hasMoreElements()
  2. nextElement()
Iterator method names have been improved. The traversing of elements can only be done once per creation.
Become an Java Expert with Our Free Course
Achieve Your Goals with this Free Training
quiz-icon

46. What are the differences between the inner class and the subclass?

Inner Class Subclass
An inner class is a class that is defined within another class. A subclass is a class that inherits properties from another class called the superclass.
It provides access rights for the class, which is nesting it, which can access all variables and methods defined in the outer class. It provides access to all public and protected methods and fields of its superclass.

47. How can we restrict a class from being inherited?

There are various ways to restrict inheritance for a class in Java. They are listed below:

  1. By using the final keyword
  2. By using private constructors
  3. By using sealed class
  1. By using final keyword: if you mark a class as final, it cannot be extended, below is the code for the same.
Java

Here, you will get an error. Since the ‘Base’ class is ‘final’, and hence it cannot be inherited. Here, if you remove the ‘final’ keyword from the ‘Base’ class, the code will work as intended.

  1. By using private constructors: if we make the constructor private of the class, then it cannot be inherited by another class. This way it cannot be instantiated outside the class.

Example

Java

Here, you will get an error since the display method in the Base class is private, hence it cannot be instantiated. If we remove the keyword ‘private’ from the method, the code will work as expected.

  1. By sealing the class: By using the seal keyword, we can restrict which classes can inherit the base class. Let’s understand it using an example:
Java

A class that wants to inherit properties from a sealed class must be declared with one of the following access specifiers: final, non-sealed, or sealed.

48. Why doesn’t Java support multiple inheritance?

Java doesn’t support multiple inheritance because of two reasons:

  1. Ambiguity
  2. Simplicity

Example:

Java

Both Intellipaat1 and Intellipaat2 classes have their own test() methods, and class C is inheriting from both. In this case, there is ambiguity because the compiler cannot determine which test() method to inherit.

To resolve this, class C must override the test() method and provide its own implementation. This way, the ambiguity is explicitly handled.

49. Can a constructor be inherited? Is it possible for a subclass to call the parent’s class constructor?

We cannot inherit a constructor in Java, However, a subclass can call the parent’s constructor using the super keyword. We do this to initialize inherited fields in the parents class by creating an instance of a subclass. By calling the parent constructor, we ensure that the parent class is initialized correctly.

50. Explain a ClassLoader in Java.

It is a part of the Java Runtime Environment which is used to dynamically load the class files into the Java Virtual Machine (JVM). The classloader triggers whenever a Java file is executed.

There are three types of classloaders in Java:

  • Bootstrap Classloader: The bootstrap loader is the superclass of extension classloader, which loads the standard JDK files from rt.jar and other core classes. The rt.jar file contains all the package classes of java.net, java.lang, java.util, java.io, java.sql, etc.
  • Extension Classloader: The extension classloader is known as the parent of the System classloaders and the child of the Bootstrap classloader. It only loads the files available in the extension classpath, which includes ‘dirs/JRE/lib/ext If the file is unavailable in the mentioned directory, the extension classloader will delegate it to the System classloader.
  • System/Application Classloader: It loads the classes of application type available in the environment variable such as -classpath, or -cp command-line option. The application classloader generates a ClassNotFoundException if the class is unavailable in the application classpath.

51. Differentiate between checked and unchecked exceptions in Java.

The compiler checks checked exceptions at compile time, meaning that programmers have to handle such exceptions with try-catch blocks or declare them using the method signature. Unlike checked exceptions, unchecked exceptions are not checked at compile-time, hence they do not require explicit handling by a programmer.

52. Can you do multithreading in Java?

Yes, multithreading is part of Java. It provides built-in support for creating and managing threads, thereby enabling multiple tasks to be executed concurrently within one program.

53. What are abstract classes in Java? Give an example.

Java Abstraction refers to showing only the function ability and behavior of methods while hiding their implementation details to the user. It focuses on what an object does rather than how it does it.

For instance, the List interface in Java represents abstraction because it describes common operations for lists without defining how those operations are carried out. Classes like LinkedList and ArrayList implement these operations differently but still follow the same interface.

54. Is it possible to declare static variables and methods in an abstract class?

Yes, we can declare static variables and methods in an abstract class by inheriting the abstract class. In java, the static variables and methods can be accessed without creating an object. You can simply extend the abstract class and use the static context as mentioned below:

Java

55. Explain Aggregation in Java.

Aggregation in Java represents a relationship where one class contains the reference of another class. In other words, it refers to a one-way relationship between two objects where the aggregate class has the instance of another class it owns. The aim is to have classes which are loosely coupled with each other.

For example, in the program below, the aggregate class Student has a one-way relationship with the class Address. Here each student having an address makes sense, but it doesn’t make sense for an address to have a student. Therefore, instead of using inheritance, aggregation is used based on how the classes are related through usage.

Java

56. What is composition in Java?

Composition refers to a relationship between two classes or objects where they are tightly coupled. In this relationship, the lifetime of the contained object depends on the lifetime of the container object. This concept is known as composition.

In simple words, the composition is a kind of aggregation that represents a strong relationship between two classes using the reference variable.

For example, if a Student  class contains an Address class and the Address class cannot exist independently without the Student class, then there is a composition relationship between them.

57. Is it possible to overload the main method in Java?

Yes, you can overload the main() method in Java using the concept of method overloading.

Below is an example of main() method overloading:

Java

This happens because the JVM only recognizes the main method with (String args[]). If you have other overloaded main methods, you have to call them explicitly within your code.

Here is the code if you want to do so,

Java

58. List the differences between constructor and a destructor.

Below are the differences between a constructor and a destructor based on various parameters:

Feature Constructor Destructor
Purpose A constructor is used to allocate the memory. A destructor is used to deallocate or free up the memory.
Arguments May or may not accept arguments. Destructors don’t accept any arguments.
Syntax class_name(arguments){

}

~class_name(){

};

Invocation Timing The constructor is called as soon as the object is created. Called at the time of program termination or when the block is exited.
Execution Order in Inheritance Constructors are executed in successive order, meaning from parent to child class. Destructors are called in the reverse order, meaning from child to parent class.
Overloading Can be overloaded. Cannot be overloaded.

59. What is a JAR file in java?

JAR (Java Archive) is a file format that aggregates many files into a single file in a format similar to a ZIP file. In other words, the JAR is a zipped file composed of java class files, metadata, and resources like text, images, audio files, directories, .class files, and more.

60. Explain the volatile keyword and its use cases.

The volatile keyword in Java ensures that updates to a variable are visible to all threads. By declaring a variable as volatile, every read loads the most recent value directly from the main memory, and every write immediately updates the main memory.

Why it is Important in Multithreading:

In a multithreaded environment, threads can cache variables locally. Without the volatile keyword, updates made by one thread may not be immediately visible to other threads, leading to inconsistent data access.

Key Use Cases:

  • Flags to manage thread execution:
    private volatile boolean running = true;
  • Double-checked locking (using volatile to avoid instruction reordering).

Limitations:

  • It does not ensure atomicity.
  • The volatile keyword is not a replacement for synchronized when there are compound actions like reading-modifying-writing involved.

61. What is the difference between throw and throws keyword?

The following are the key differences between the throw and throws keyword:

throw throws
The throw keyword is used to explicitly throw an exception inside a program or a block of code. Throws keyword is used to declare an exception in the method signature that might occur during the compilation or execution of a program.
Can throw only unchecked exceptions. Can be used for both checked and unchecked exceptions.
throw keyword followed by an instance variable. throws keyword followed by the exception class names.
throw keyword is used to throw only a single exception. throws can be used to throw multiple exceptions.

62. What is the instanceof operator used for?

The instanceof operator is used to check whether an object is an instance of a particular class or implements a specific interface. It is commonly used to verify an object’s type before downcasting.

Syntax:

if (obj instanceof MyClass) {
    MyClass myObj = (MyClass) obj;
}

Use Case:

  • Polymorphic functionality when various types are accessed through a superclass or interface.
  • Casting objects safely while programming with Object references.

Best Practice:

Use polymorphism and method overriding instead of overusing instanceof, as the latter may become a code smell for certain OOP designs.

63. How does the Comparator interface differ from Comparable?

Feature Comparable Comparator
Package java.lang java.util
Method compareTo(T o) compare(T o1, T o2)
Defined In Same class being compared Separate or anonymous class
Sorting Logic Natural/default order Custom or multiple orderings
Flexibility One comparison strategy only Multiple strategies possible

Comparable Example:

public class Student implements Comparable {
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}

Comparator Example:

Comparator byName = (s1, s2) -> s1.name.compareTo(s2.name);

Use Case:

Use Comparable for natural ordering. Use Comparator for flexible, dynamic ordering (particularly with varying fields).

64. What is a ConcurrentModificationException, and how can it be avoided?

ConcurrentModificationException is raised when a thread is modifying a collection while iterating over its elements with fail-fast iterators like ArrayList, HashMap, etc.

Common Cause:

for (String s : list) {
    list.remove(s); // Unsafe!
}

How to Avoid It:

Use Iterator’s remove() method:

Iterator it = list.iterator();
while (it.hasNext()) {
    if (condition) it.remove();
}

Use concurrent collections like CopyOnWriteArrayList, ConcurrentHashMap when working in multithreaded environments.

Collect and remove later:

List toRemove = new ArrayList<>();
for (String s : list) {
    if (condition) toRemove.add(s);
}
list.removeAll(toRemove);

65. What is the difference between Jakarta EE and legacy Java EE?\

Jakarta EE is a new Java EE that is more current. Java EE was initially developed by Oracle, but Jakarta EE has been transferred to the Eclipse Foundation, which is more open, innovative at a faster pace, and is better suited to cloud-native architectures.

Key Differences:

Feature Java EE Jakarta EE
Governance Oracle Eclipse Foundation
Package Names javax.* jakarta.* (from Jakarta EE 9 onwards)
Community Involvement Limited Open, community-driven
Cloud & Microservices Less emphasis Strong microservices and cloud focus
Innovation Pace Slower More agile and modular
Compatibility Stable, but less future-proof Backward-compatible and forward-looking

66. How do you create an immutable class in Java?

An immutable class is that class whose state (values of fields) cannot be modified once an object is created. Java’s String class is a prime example of an immutable class.

Steps to create an immutable class:

  1. Declare the class as final so it can’t be subclassed.
  2. All fields should become final and private.
  3. Don’t give them setters.
  4. Initialize fields only in the constructor.
  5. For mutable fields, create copies in the getter and the constructor.

Example:

Java

Immutable objects are naturally thread-safe and prevent unwanted side effects, making them ideal for concurrent programming.

67. What is a static nested class?

The static nested class is a nested class that is declared with a static modifier inside another class. It does not have direct access to instance members of an outer class unlike non-static inner classes.

Key Points:

  • It acts like a top-level class but is scoped inside the outer class.
  • Most handy when grouping classes that are utilized solely by the containing class.
  • Can reference static members of its enclosing class.

Example:

Java

Usage:

Outer.StaticNested obj = new Outer.StaticNested();
obj.display();

68. How does the synchronized keyword work in methods?

The synchronized keyword in Java is utilized to manage the multiple threads’ access to a method or section of code to provide mutual exclusion and visibility.

Synchronized Instance Method:

public synchronized void method() {
    // thread-safe logic
}
  • Acquires a lock on this current object.
  • At most, a single thread can invoke any synchronized method on an object.

Synchronized Static Method:

public static synchronized void method() {
    // logic
}
  • Obtains a lock on the class object (ClassName.class) rather than on an instance.

Use Cases:

  • Avoid race conditions.
  • Maintain data consistency in concurrent situations.

Limitation:

  • If used incorrectly, it can lead to performance bottlenecks and deadlocks.

69. What is the @Override annotation, and why is it used?

The @Override annotation signifies that a method is overriding a method in its superclass or implementing an interface method.

Benefits:

  • Compile-time checking: The compiler will issue an error on encountering a method that does not properly override a method from its parent.
  • Enhances readability: Makes it clear that the method is within the inheritance hierarchy.

Example:

Java

Without @Override, typos can silently result in unexpected behavior:

// Misspelled method name — no compile error without @Override
void speek() { ... }

70. What is the difference between Stack and Heap Memory in Java?

The key differences between Heap and Stack memory in Java are:

Stack Memory Heap Memory
Smaller in size. Larger in size.
Exists until the end of the thread. Lives from the start till the end of application execution.
Objects stored in Stack can’t be accessed by other threads. Globally accessible.
Follows LIFO (Last In First Out) order. Dynamic memory allocation.
The variables in Stack memory are only visible to the owner thread. Heap memory is visible to all the threads.
Continuous memory allocation. Random order memory allocation.
Less flexible, one cannot alter the allocation memory. Users can alter the allocated memory.
Fast access, allocation, and deallocation. Slow access, allocation, and deallocation.

71. What is a Predicate in Java?

Predicate is a functional interface added in Java as part of the package java.util.function. It is a representation of a one-argument boolean-valued function.

Method Signature:

@FunctionalInterface
public interface Predicate {
    boolean test(T t);
}

Example Usage:

Predicate startsWithA = str -> str.startsWith("A");
System.out.println(startsWithA.test("Apple")); // true
System.out.println(startsWithA.test("Banana")); // false

Use Cases:

  • Applied with streams, filtering, and conditional logic.

Example with streams:

List names = List.of("Alice", "Bob", "Ankit");
names.stream()
     .filter(name -> name.startsWith("A"))
     .forEach(System.out::println);

72. How does HashSet internally store elements?

Internally, HashSet uses a HashMap to store its elements. When an element is added to a HashSet, it is added as a key in the underlying HashMap with a constant dummy value (PRESENT) associated with it.

Internal Representation:

private transient HashMap<E, Object> map;
private static final Object PRESENT = new Object();

Working:

  • It relies on hashCode() and equals() methods to provide uniqueness.
  • Even when two objects share identical hash codes but are not equal through equals(), both can still be cached.

Example:

HashSet set = new HashSet<>();
set.add("Java"); // Stored as map.put("Java", PRESENT)

73. What is the diamond operator (<>) in generics?

The diamond operator (<>), added in Java 7, has made instantiation of generic types easier by enabling the compiler to deduce the type arguments.

Before Java 7:

List list = new ArrayList();

With Diamond Operator (Java 7+):

List list = new ArrayList<>();

Benefits:

  • Minimizes boilerplate.
  • Enhances readability.
  • Prevents duplicate declarations of types.

74. How do you create a custom exception in Java?

You can create a custom exception by subclassing the Exception or RuntimeException class.

Checked Exception:

Java

Unchecked Exception:

Java

Usage:

Java

Custom exceptions assist in offering descriptive context to error handling, particularly for domain-specific projects.

75. How does structured concurrency simplify error handling?

Structured concurrency is a structured approach that enables threads and tasks to be managed efficiently by specifying the manner in which they are started, supervised, and finished within a clearly defined scope. This model makes it easy to deal with errors and manage tasks in concurrent programs.

In what way it makes error handling easy:

  • In case one task goes wrong, the system will be able to cancel all other tasks which are running at the same time without the user being involved.
  • The exceptions might be handled at a central point by the parent task or scope.
  • It is not going to allow thread leaks and tasks without owners to exist.

Example using StructuredTaskScope (Java 21+):

Java

Note: This requires Java 21+ with –enable-preview for Structured Concurrency.

76. What is a NullPointerException in Java?

A NullPointerException (NPE) is a run-time exception thrown when the program attempts to:

  • Accessing a field or method on a null reference.
  • Use NULL where an object is expected.

Common Scenarios:

String name = null;
System.out.println(name.length()); // Throws NullPointerException

How to Avoid:

  • Always check for null when invoking methods
  • Use Optional for safe-null design
  • Declare variables correctly

NPEs are among Java’s most frequent bugs. Defensive programming and features such as Optional can lower such occurrences significantly.

77. Explain the difference between FileInputStream and FileReader.

FileInputStream and FileReader are both utilized for reading data from a file, but they differ in type of data they handle:

Feature FileInputStream FileReader
Data Type Reads raw bytes Reads character data
Suitable For Binary files (images, videos, PDFs) Text files (UTF-16, ASCII, etc.)
Inherits From InputStream Reader
Encoding Handling Doesn’t handle character encoding Handles character encoding

Example:

// FileInputStream
FileInputStream fis = new FileInputStream("image.jpg");
// FileReader
FileReader fr = new FileReader("notes.txt");

78. What is the purpose of the java.util.Optional class?

Optional<T> is a container class that can or cannot have a non-null value. It prevents null pointer exceptions and promotes better practices for handling null values.

Use Case:

Rather than returning null, return an Optional<T>.

Example:

Java

Benefits:

  • Decreases likelihood of NullPointerException.
  • Encourages clear null checks with ifPresent(), orElse(), map(), etc.

79. How do you use varargs (variable arguments) in Java?

Varargs (…) enable a method to take zero or more parameters of a particular type. They are useful when the number of method parameters is variable and not known in advance.

Syntax:

Java

Rules:

  • Only one varargs parameter is allowed.
  • It has to be the final parameter in the method signature.

80. What is the difference between poll() and remove() in a Queue?

Method Description Throws Exception if Queue is Empty
poll() Retrieves and removes the head, or returns null if empty  No (Returns null)
remove() Retrieves and removes the head, or throws NoSuchElementException if empty Yes

Example:

Java

When to Use:

  • Use poll() when you wish to safely search for an element.
  • Use remove() when you are certain that the queue is not empty.

81. What is a BiFunction in Java?

BiFunction<T, U, R> is a functional interface in package java.util.function that defines a function that accepts two parameters of types T and U and has an output of type R.

Syntax:

Java

Example:

Java

Use Cases:

  • Merging two values (for instance, keys and values in maps).
  • Functional-style computation with two inputs.

82. How does the flatMap() method work in streams?

The Java Streams flatMap() method is utilized to flatten hierarchical structures, often by transforming a stream of collections (e.g., lists) into a single stream of their individual elements.

Behavior:

  • Maps every element to a Stream.
  • Flattens the outcome into one Stream.

Example:

Java

When to Use:

  • When dealing with nested collections.
  • To prevent Stream<Stream<T>> nesting.

83. What is the @FunctionalInterface annotation?

@FunctionalInterface is an annotation that is used to specify that an interface is meant to be a functional interface (an interface with only one abstract method).

Purpose:

  • Tells the compiler to implement a functional interface contract.
  • Facilitates use with lambda expressions and method references.

Example:

Java

Benefits:

  • Improves code clarity.
  • Prevents accidental addition of multiple abstract methods.

84. How do you handle time zones using the java.time API?

The Java.time API introduces classes such as ZonedDateTime, ZoneId, and OffsetDateTime to manage date and time with zone information support.

Example:

Java

Convert Between Time Zones:

Java

Key Classes:

  • ZoneId – Represents a time zone (e.g., “America/New_York”)
  • ZonedDateTime – Date/time with a time zone
  • OffsetDateTime – Date/time with an offset from UTC

85. List the differences between ArrayList and LinkedList in Java?

ArrayList is a dynamic array that stores elements and supports fast random access. It is better suited for element traversal and frequent access. Conversely, LinkedList is a linked list that stores its elements in the form of nodes, making it very suitable for high-speed insertion and deletion operations. Therefore, it can be useful where there are many additions or removals.

If you want fast access and traversal through elements, then use an ArrayList; otherwise, for fast insertion or deletion of elements, use LinkedList.

86. Explain the use of copy constructors in Java.

Java does not support a copy constructor. However, you can manually create a copy by assigning the instance variables of one object to another. Below is an example of such a method:

Java

87. List the differences between aggregation and composition.

Below are the key differences between aggregation and composition in Java:

Aggregation Composition
The parent class has a one-way relationship with the child class. In composition, the parent class owns the child class.
Aggregation is denoted by an empty diamond in UML (Unified Modeling Language) notation. Denoted by a filled diamond in the UML notation.
Child class has its own life cycle. Child class doesn’t have a lifetime.
Aggregation is a weak relationship. For example, a bike with an indicator is aggregation. Composition is a stronger relationship. For example, a bike with an engine is a composition.

88. Explain the purpose of object cloning in Java.

Object cloning means creating the exact copy of an existing object. In java programming, object cloning can be done using the clone() method. To create the object clone, you should implement the java.lang.Cloneable interface, otherwise the clone() method generates a CloneNotSupportException.

The syntax for the clone method is:

protected Object clone() throws CloneNotSupportedException

89. Explain various exception handling keywords in Java.

Java has three exception handling keywords:

  • try: Whenever a code segment has a chance of an error or any abnormality, it can be placed inside the try block. In case of any error, the try block will send the exception to the catch block.
  • catch: Catch handles the exception generated in the try block. It comes after the try block.
  • finally: The final keyword ensures that the segments execute despite the errors and exceptions processed by the try & catch block. The final keyword comes either after the try block or both the try and the catch block.

90. List the differences between this() and super() keywords in Java.

this() super()
Represents the present instance of a class Represents the current instance of the parent class.
Calls the default constructor. Calls the base class constructor.
Used to point to the current class instance. Used to point to the instance of the superclass.

91. Is it possible to import the same package/class twice? Will the JVM load the package twice at runtime?

A package or class can be inherited multiple times in a program code. JVM and compiler don’t create any issue. Moreover, JVM automatically loads the class internally once, regardless of times it is called in the program.

92. List the differences between HashMap and HashTable in Java?

Both HashMap and HashTable are used to save key/value pairs, but with some differences:

  • HashMap does not have a synchronization feature, making it not thread-safe, while HashTable does.
  • HashMap allows null keys as well as values, but HashTable does not allow any null key or value.

Generally, HashMap is preferred in non-thread-safe scenarios where performance is important; on the other hand, HashTable is recommended for multi-threaded environments where thread safety is required.

93. Explain the use of access modifiers in Java.

Public, protected, default (no modifier), and private are four access modifiers in Java.

  • Public: access from anywhere.
  • Protected: access to the same package or other packages (subclasses).
  • Default (no modifier): Only access in the created package.
  • Private: No one else but the declaring class will be able to obtain it.

Use public if you want your member to be accessed from anywhere, protected when you wish to restrict access to subclasses and classes within the same package, default when you want access at the package level, and private when you only want access within the declaring class.

94. What is the difference between static loading and dynamic class loading?

  • Static loading: In static loading, classes are loaded statically with the operator ‘new.’
  • Dynamic class loading: It is a technique for programmatically invoking the functions of a class loader at runtime. The syntax for this is as follows:
Class.forName (Test className);

95. What methods are available in Java to find the actual size of an object on the heap?

In the Java programming language, you cannot accurately determine the exact size of an object located in the heap.

96. Explain the difference between transient and volatile keywords in Java.

Transient: In Java, it is used to specify whether a variable is not being serialized. Serialization is a process of saving an object’s state in Java. When we want to persist the object’s state by default, all instance variables in the object are stored. In some cases, we want to avoid persisting a few variables because you don’t have the necessity to transfer across the network. So, you can declare those variables as transient.

If the variable is confirmed as transient, then it will not be persisted. The transient keyword is used with the instance variable that will not participate in the serialization process. We cannot use static with a transient variable as they are part of the instance variable.

Volatile: The volatile keyword is used with only one variable in Java, and it guarantees that the value of the volatile variable will always be read from the main memory and not from the thread’s local cache; it can be static.

97. What is the use of synchronized blocks?

The synchronized block in Java is used to provide exclusive access to shared resources by locking a specific object. It helps prevent thread interference and ensures thread safety when multiple threads access the same resource. Unlike synchronized methods, the scope of a synchronized block is limited to a specific section of code, offering more fine-grained control over synchronization.

98. What is an interface in Java?

Java interface is a completely abstract class referred to as a collection of abstract methods, static methods, and constants. It acts as a blueprint of a class that groups related methods or functions with empty bodies.

Below is an example of a java interface:

Java

99. What is the purpose of servlets in java?

Servlets are server-side Java programs used to create web servers and application servers. They can handle the incoming requests from the web clients, process them and send back responses through the web server.

Several packages like javax.servlet and javax.servlet.http provide the appropriate interfaces and classes for creating servlets. All the servlets should implement the javax.servlet.Servlet interface, often by extending the HttpServlet class, which is part of the Java Servlet API.

The execution of a servlet involves five distinct steps:

  • The clients initiate the request by sending it to the web server or application server.
  • The web server receives the request from the clients.
  • The servlet processes the request, carrying out the necessary computations, and generates the corresponding output.
  • Subsequently, the servlet sends the processed request back to the web server.
  • Finally, the web server dispatches the request to the clients, and the resulting output is displayed on the client’s screen or any other designated device.

100. Explain the use of a Request Dispatcher in Java.

The request dispatcher serves as an interface utilized to effectively redirect requests to various resources within the application itself. These resources may include Java Server Pages (JSP), images, HTML files, or other servlets.

Moreover, it facilitates the integration of responses from one servlet into another, guaranteeing that the client receives output from both servlets. Additionally, it allows for the seamless forwarding of client requests to the next servlet in the specified sequence.

There are two methods defined in a Requestdispatcher interface:

  • void include(): It includes the content of the resource before sending the response.
  • void forward(): the method forwards the request from one servlet to another resource like the JSP, image, etc on the server.

101. What are cookies in Servlets?

Cookies are text files which are stored on the client side (web browser) and maintain information about the state or session. They are required by servers, since HTTP protocols are stateless in nature. They help the server to know the user preferences, session information, etc.

Cookies have a size limitation of 4KB. They can be cleared or disabled by the user.

102. List the differences between ServletContext vs. ServletConfig.

The differences between ServletContext and ServletConfig are as follows:

ServletContext ServletConfig
It is a global object, which helps get information/configuration for the entire application. It is not global, and only valid per servlet. It is used to pass initialisation parameters valid for only a specific servlet.
Shared across all Servlets. Limited to a single servlet.
Typically only one ServletContext per web application. Only one ServletConfig per servlet.
It is used for global level parameters such as database config or global settings which many servlets want to share. Used for servlet level parameters, which has to be used on that specific servlet..

103. What is a thread lifecycle in Java?

There are 5 states in a thread lifecycle, let’s understand each one of them:

  • New: The thread lies in the new state after it has been created. It remains in the new state until you start the execution process.
  • Runnable: At runnable state, the thread is ready to run at any point in time or it might have started running already.
  • Running: The scheduler picks up the thread and changes its state to running, the CPU starts executing the thread in the running state.
  • Waiting: While being ready to execute, another thread might be running in the system. So, the thread goes to the waiting state.
  • Dead: Once the execution of the thread is finished, its state is changed to the dead state, which means it’s not considered active anymore.

104. What are the different types of Exceptions in Java.

There are two types of Exceptions in Java Programming:

  • Built-in Exception: These exceptions are the exceptions available in the standard package of java lang. They are used for certain errors with specific exceptions as mentioned below:
    • ArithmeticException: thrown for the errors related to the arithmetic operations.
    • IOException: thrown for failed input-output operations.
    • FileNotFoundException: raised when the file is not accessible or does not exist.
    • ClassNotFoundException: exception is raised when the compiler is unable to find the class definition.
    • InterruptedException: raised when a thread is interrupted.
    • NoSuchFieldException: raised when a class or method doesn’t have a variable specified.
    • NullPointerException: thrown while referring to the variable or values of a null object.
    • RuntimeException: raised for the errors occurring during the runtime. For example, performing invalid type conversion.
    • IndexOutOfBoundsException: raised when the index of the collection like an array, string, or vector is out of the range or invalid.
  • User-defined Exceptions: User-defined exceptions are the exceptions thrown by the user with custom messages. These are used for cases where the in-built Exception might not be able to define the error.

105. Can we write multiple try-catch blocks in java? If yes, how?

In Java, we can include multiple catch statements for a single try block. This approach allows multiple exceptions to be handled separately within a single try block.

Below is the program for multiple catch statements:

Java

106. What is HTTP Tunneling?

HTTP tunneling refers to encapsulating non HTTP data into HTTP/HTTPS. This is generally done to bypass firewalls, network restrictions or proxies where only HTTP or HTTPS traffic is allowed.

It works in a client-server setup like this:

  1. The client encapsulates the non-HTTP data into HTTP/HTTPS data and sends it through the firewall to the server.
  2. The server decrypts the encapsulated data, and forwards it to the destination. Once the response is received from the destination, the server again packs the data into HTTP/HTTPS and sends it to the client.

107. Explain the use of collections in Java.

A collection is a framework that provides a set of classes and interfaces, which are used to manage, store and manipulate data for a group of objects in Java.

Java Collections can be used for performing a variety of operations such as:

  • Search
  • Sort
  • Manipulate
  • Delete

108. How would you implement a custom collector?

One can write a custom collector in Java by using the Collector.of() factory method or by implementing the Collector interface. This is especially helpful when the built-in collectors are not enough to satisfy particular transformation or aggregation requirements.

Example: Let us say you want to implement a collector that concatenates uppercase strings separated by commas.

Java

This collector:

  • Creates a StringJoiner
  • Inserts uppercase strings
  • Combines results from parallel streams
  • Turns the result into a String

Use cases: Custom collectors can be used when dealing with formatting, summarizing, or reducing complicated objects in stream pipelines.

109. How to write the string reversal program in Java without using the in-built function?

There are two ways we can reverse a string in java without using the in-built function:

  • Using a loop by printing string backward, one character each time.
  • Using a recursive function and printing the string backward.

In the following program, you can see both ways to print the string in reverse order:

Java

110. What is a StringJoiner in java? Explain with a sample code.

StringJoiner is a class in Java that is used to join multiple strings together, optionally adding a delimiter between them. Here’s an example code that demonstrates the usage of StringJoiner:

Java

111. How do you read and write files using java.nio.file?

Java.nio.file in Java 7 and later is the preferred method of dealing with file I/O because of its simplicity and efficiency. It is part of the New I/O (NIO) API and supports non-blocking, memory-efficient I/O.

To read a file:

Path path = Paths.get("sample.txt");
List lines = Files.readAllLines(path, StandardCharsets.UTF_8);

To write to a file:

Path path = Paths.get("output.txt");
Files.write(path, Arrays.asList("Line 1", "Line 2"), StandardCharsets.UTF_8);

The java.nio.file package also supports:

  • File attributes
  • Stream-based I/O
  • Directory traversal

These methods are widely used in modern Java development, especially for building file processing systems or working with large data sets.

112. What is a lambda expression, and how is it used in Java?

A Java lambda expression is a concise block of code that can be passed around and executed. Introduced in Java 8, it is mainly used to implement  functional interfaces, interfaces with a single abstract method.

The syntax:

(parameter) -> { body }

For example:

Runnable r = () -> System.out.println("Hello from Lambda!");

Lambda expressions are widely employed in functional programming, particularly with Java Stream API, to perform operations such as filtering, mapping, and iterating. They make the code more readable, concise, and highly expressive, particularly when dealing with collections and concurrency APIs.

113. What are streams in Java, and how are they used?

Java Streams are a sequence of elements that can support functional-style operations like filter, map, reduce, and collect. The Java Stream API improves collection processing by enabling declarative, pipelined operations on data structures like  List and Set.

Here is a sample code:

Java

The streams are either parallel or sequential and enable parallel processing to optimize the execution. Both are extremely useful when combined with method references and lambda expressions and encourage clean, functional style programming in Java.

114. Explain the concept of autoboxing and unboxing.

Autoboxing and unboxing are significant additions made to Java 5 to connect primitive data types and their corresponding wrapper classes.

  • Autoboxing is the process where a primitive type like int is automatically converted to its equivalent wrapper class such as Integer.
  • Unboxing is converting an Integer back to an int.

Example:

Integer x = 10;  // autoboxing
int y = x;       // unboxing

These conversions are useful with collections like ArrayList, where objects are needed instead of primitives. They make your code more readable and less cluttered with boilerplate, although be mindful of the performance ramifications when in a tight loop.

115. What is the purpose of the transient keyword in serialization?

In Java, if a class implements Serializable, all of its fields are eligible to be serialized by default. However, if you want to prevent a particular field from getting serialized, you make that field transient.

For instance:

 private transient String password;

This instructs the Java Serialization API to skip this field while converting the object to a byte stream. It is usually employed to shield sensitive information (like a password, a session token) or to skip non-serializable objects like Thread, Socket, etc.

In summary, transient is a tool of optimization and security for use when dealing with serialization in Java.

116. What is the difference between a String and a StringBuilder?

The main distinction between String and StringBuilder in Java is related to immutability and efficiency.

  • A String is immutable, meaning its value cannot be altered once it is created. Operations like concatenation produce a new String object, which can be memory-inefficient.
  • A StringBuilder is mutable, or able to change its value without the creation of a new object. It is much more efficient to use in loops.

Example:

Java

Java

Therefore, whenever you are performing heavy string manipulations, always use StringBuilder or StringBuffer instead of a regular String to improve performance and reduce memory overhead.

117. What is the Difference Between List and Set in Java?

The main difference between List and Set in Java is ordering and duplicates.

Feature List Set
Duplicates Allows duplicates Does not allow duplicates
Ordering Maintains insertion order May not maintain order (depends on implementation)
Access Indexed access via get(int) No index-based access
Implementations ArrayList, LinkedList, Vector HashSet, LinkedHashSet, TreeSet

A List is employed when there is a need to maintain order and permit duplicates. A Set is preferable when there is a need for distinct elements and no order (or sorted order by TreeSet) is desired.

118. How Does List Differ from Map in Java?

List and Map serve entirely different purposes in Java Collections Framework.

Feature List Map
Data Type Collection of elements Collection of key-value pairs
Access Accessed by index Accessed by key
Duplicates Allows duplicate elements Keys must be unique; values can duplicate
Implementations ArrayList, LinkedList HashMap, LinkedHashMap, TreeMap

List is a linear data collection, and Map is a key-value mapping. Use a Map whenever data lookup by a distinct key is needed, like in caching or lookup applications.

119. Compare Queue and Stack in Java

Queue and Stack are both linear data structures but follow different ordering principles.

Feature Queue Stack
Ordering FIFO (First-In-First-Out) LIFO (Last-In-First-Out)
Insertion Via add() or offer() at tail Via push()
Removal Via poll() or remove() at head Via pop()
Use Cases Task scheduling, resource sharing Recursion, backtracking, parsing

In Java, Queue is denoted by interfaces such as Queue and classes such as LinkedList or PriorityQueue. Stack is a java.util class, although Deque should be used instead of Stack since it offers improved performance and thread-safety.

120. What distinguishes PriorityQueue from TreeSet in Java?

While both PriorityQueue and TreeSet maintain sorted elements, they differ in structure, usage, and uniqueness constraints.

Feature PriorityQueue TreeSet
Duplicates Allows duplicates Does not allow duplicates
Ordering Natural or comparator-based priority Natural or custom order
Access No random access; only peek/poll head Can navigate using methods like first(), last()
Underlying Structure Binary heap Red-Black tree (self-balancing BST)

Use PriorityQueue whenever efficient access to the smallest or largest element is desired, or especially when scheduling or prioritization of tasks is involved. Use TreeSet when sorted distinct elements are desired with quick lookup and range queries.

121. Differentiate Between Singly Linked List and Doubly Linked List

The main difference between a singly linked list and a doubly linked list lies in how nodes reference each other.

Feature Singly Linked List Doubly Linked List
Node Structure Contains data + next pointer Contains data + next + previous pointer
Traversal Direction Only forward Forward and backward
Memory Usage Less memory (one pointer per node) More memory (two pointers per node)
Insertion/Deletion Slower for reverse ops Faster for both directions
Implementation Simpler More complex

A singly linked list is light and suitable for data that will be viewed in only one direction. A doubly linked list is more versatile when bi-directional traversal or removals are needed.

122. What is the difference between Fail-Fast and Fail-Safe iterators in Java?

Fail-Fast and Fail-Safe are terms that describe how iterators will react if the collection is structurally changed while iterating.

Feature Fail-Fast Fail-Safe
Behavior on Modification Throws ConcurrentModificationException Works safely without exception
Examples ArrayList, HashSet, HashMap (normal) CopyOnWriteArrayList, ConcurrentHashMap
Underlying Copy Works on the same collection Works on a cloned copy of the collection
Performance Faster Slightly slower due to cloning

Use Fail-Safe iterators in multithreaded environments. Fail-Fast is useful to catch issues early at development time.

123. How Does HashMap Differ from TreeMap in Java?

HashMap and TreeMap are both implementations of the Map interface but are very different in behaviour, performance and ordering.

Feature HashMap TreeMap
Ordering No guaranteed order Sorted based on keys (natural/comparator)
Performance O(1) average for get/put O(log n) for get/put
Null Keys Allows one null key Does not allow null keys
Use Case Fast lookup, unordered data Sorted map for range queries or ordered traversal

Use HashMap for better performance. Use TreeMap if a sorted key-value map is desired.

124. Compare Queue and Deque in Java.

Queue and Deque both belong to the java.util package but Deque is more versatile.

Feature Queue Deque (Double-Ended Queue)
Direction Elements added at rear, removed from front. Elements can be added/removed from both ends.
Typical Usage FIFO (First-In-First-Out) FIFO and LIFO (supports both)
Methods add(), poll(), peek() addFirst(), addLast(), removeFirst()
Implementations LinkedList, PriorityQueue ArrayDeque, LinkedList

Use Queue when performing simple FIFO operations. Use Deque if you need both ends to be flexible, like when implementing a stack or a sliding window.

125. What are the differences between HashSet and TreeSet in Java?

Both TreeSet and HashSet are implementations of the Set interface but vary based upon ordering and efficiency.

Feature HashSet TreeSet
Ordering No specific order Sorted order based on natural or custom comparator
Performance O(1) for basic operations (average) O(log n) for all operations
Null Elements Allows one null element Does not allow null elements
Use Case Fast access, unordered set Sorted set with range query support

Use a HashSet where there are requirements of performance and no ordering. Use TreeSet for sorted entries and navigation methods such as headSet() or tailSet().

126. What are the different thread priorities in Java, and what is the default priority assigned by the JVM?

In Java, every thread has a priority value ranging from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY). The default priority that the JVM gives to every thread is 5 (NORM_PRIORITY).

Thread Priority Constants:

  • MIN_PRIORITY = 1
  • NORM_PRIORITY = 5 (default)
  • MAX_PRIORITY = 10

Significance: Priority of the thread will affect the thread scheduler to select the thread to be executed, but not necessarily-governed by the JVM’s underlying operating system level thread scheduling mechanism. It is a suggestion, not a requirement.

127. Why is garbage collection necessary in Java?

Java garbage collection (GC) plays a crucial role in automatic memory management. It releases memory by deleting objects that are not accessible to any running thread or static reference.

Why it’s important:

  • Avoids memory leaks.
  • Reduces manual memory deallocation needs (such as in C/C++).
  • Helps to ensure runtime performance and avoid OutOfMemoryError.

Garbage collection makes Java programs less prone to errors with respect to memory management.

128. What classes are available in the java.util.regex package?

The java.util.regex package offers classes to match patterns by means of regular expressions in Java.

Primary classes:

  1. Pattern: The compiled form of a regular expression.
  2. Matcher: Used to conduct match operations on input strings against a Pattern.
  3. PatternSyntaxException: Unchecked exception that occurs when a pattern syntax is incorrect.

Usage Example:

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("There are 123 apples");
System.out.println(matcher.find()); // true

These classes are powerful tools for text processing, validation, and data extraction in Java.

129. How do you write a regular expression to validate a password in Java?

A good password should have at least an uppercase character, a lowercase character, a digit, a special character, and a minimum length. Below is a regex pattern that meets that requirement:

Regex for Password Validation:

String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@#$%^&+=!]).{8,}$";

Explanation:

  • (?=.*[a-z]) – at least one lowercase letter
  • (?=.*[A-Z]) – at least one uppercase letter
  • (?=.*\\d) – at least one digit
  • (?=.*[@#$%^&+=!]) – at least one special character
  • .{8,} – minimum length of 8 characters

Validation Example:

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher("Secure@123");
System.out.println(matcher.matches()); // true

This ensures the password is secure and meets common complexity standards.

130. What is JDBC in Java?

JDBC (Java Database Connectivity) is an API that allows Java programs to communicate with a database using SQL queries. It offers a common interface to connect to relational databases such as MySQL, PostgreSQL, Oracle, etc.

Key Features:

  • Run SQL queries (SELECT, INSERT, UPDATE, DELETE)
  • Retrieve results through ResultSet.
  • Manage transactions.
  • Utilize prepared and callable statements to help ensure security and stored procedures.

Typical JDBC Workflow:

  1. Load the JDBC driver.
  2. Create a connection by using DriverManager.
  3. Create a Statement or PreparedStatement
  4. Run queries and process the result set.
  5. Close all windows.

131. What is a JDBC Driver?

A JDBC driver is a program component that enables Java applications to interface with a targeted database. It is a bridge between the database server and the Java application.

Types of JDBC Drivers:

  1. Type 1: JDBC-ODBC bridge (deprecated).
  2. Type 2: Native-API driver (requires native DB libraries).
  3. Type 3: Network Protocol driver (middleware-based).
  4. Type 4: Thin driver (pure Java, platform-independent, most common).

Example: For MySQL, you use the MySQL JDBC Driver:

Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "pass");

Today, type 4 drivers are extensively employed since they are independent of external dependencies and provide improved performance.

132. What are the steps to connect to a database using JDBC in Java?

To connect a Java application to a database using JDBC (Java Database Connectivity), the following steps are typically followed:

  1. Import JDBC Packages:
import java.sql.*;
  1. Load and Register JDBC Driver:
Class.forName("com.mysql.cj.jdbc.Driver"); // MySQL driver
  1. Establish the Connection:
Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/dbname", "username", "password");
  1. Create a Statement:
Statement stmt = conn.createStatement();
  1. Execute SQL Queries:
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
  1. Process the Results:
while(rs.next()) {
    System.out.println(rs.getString("username"));
}
  1. Close the Connection:
rs.close();
stmt.close();
conn.close();

These steps form the standard flow for database connectivity and query execution in Java.

133. What are the components of the JDBC API?

The JDBC API offers a collection of interfaces and classes that Java applications can use to connect and interact with relational databases. The main components are:

  1. DriverManager: Handles the collection of JDBC drivers and creates a connection.
  2. Connection: A connection to a database.
  3. Statement: Used to run static SQL queries.
  4. PreparedStatement: Used to execute parameterized SQL statements.
  5. Callable Statement: Used to call stored procedures.
  6. ResultSet: The result of a query.
  7. SQLException: SQLException handles database access exceptions.

They enable you to execute CRUD operations, maintain transactions, and work with result sets effectively.

134. What is the purpose of the JDBC Connection interface?

The JDBC Connection interface signifies an established connection between a Java program and a database.

Key Purposes:

  • Enables communication with the database.
  • Permits execution of SQL statements via Statement, PreparedStatement, or CallableStatement.
  • Supports transactional management with commit(), rollback(), and setAutoCommit(false).
  • Provides metadata access through getMetaData().

Example:

Connection conn = DriverManager.getConnection(url, user, pass);

Once created, the Connection object serves as the basis of all database operations in a JDBC program.

135. What is the purpose of the toString() method in Java?

The toString() method in Java returns a string describing an object. It is part of the Object class and is usually overridden to provide a more meaningful result.

By default, it returns the class name of the object and the hash code, such as:

Car@1a2b3c

But when overridden:

@Override
public String toString() {
    return "Car model: " + this.model;
}

The method is generally very helpful in debugging, logging, and object printouts. This enhances maintainability and readability.

136. What considerations should be made when implementing the equals() method in Java?

When overriding the equals() method in Java, a few key rules have to be followed to make it function appropriately and consistently:

  1. Reflexive: a.equals(a) must return true.
  2. Symmetric: b.equals(a) should be true if and only if a.equals(b) is true
  3. Transitive: If a.equals(b) and b.equals(c), then a.equals(c) must be true.
  4. Consistent: Identical calls should give the same response unless there are object property updates.
  5. Non-nullity: a.equals(null) must return false.

Also, whenever overriding equals(), you should override hashCode() to ensure the contracts are consistent, particularly if the object is employed in hash-based collections such as HashMap or HashSet.

137. What is the purpose of the hashCode() method in Java?

Java’s hashCode() method returns an integer hash code that denotes the object’s memory location or, more practically, its location within a hash-based collection.

It is essential for both performance and accuracy in data structures such as:

  • HashMap
  • HashSet
  • Hashtable

Key Rule:

If two objects are equal to each other based on equals(), they should have the same hashCode().

Example override:

@Override
public int hashCode() {
    return Objects.hash(id, name);
}

Incorrect implementation of hashCode() will cause unexpected effects in hash collections, such as missing or duplicated entries.

138. Can a superclass reference variable hold an object of a subclass in Java?

Yes, in Java, a reference variable of a superclass can point to a subclass object. This is known as polymorphism, and dynamic method dispatch occurs at runtime.

Java

Java supports calling overridden methods at runtime, enabling runtime polymorphism, which promotes flexible and scalable program design.

139. What are some complexities associated with interfaces in Java?

Interfaces provide flexibility but introduce a couple of complexities in Java programming:

  1. Diamond Problem with Default Methods: If two interfaces have a common default method, the default method in the class should be overridden to avoid ambiguity.
  2. Refactoring Challenges: Introducing a new method to an interface breaks all the implementation classes (except for a default method).
  3. Lack of State: Interfaces cannot store state (except constants), and that makes the design less intuitive in certain cases.
  4. Overuse Leads to Complexity: Excessive use of interfaces tends to complicate the code unnecessarily, particularly if applied to plain structures.

Despite all this, interfaces continue to be a foundation of Java’s abstraction and decoupling strategies, especially within frameworks and complex systems.

140. Can you extend an interface in Java?

Yes, a Java interface can inherit one or more interfaces with the extends keyword. This is a way Java supports interface inheritance so that different interface types are combined under a common abstraction.

Java

Any class that extends C will have to supply all methods of A, B, and C. This encourages modular programming and permits interface hierarchies to change gracefully.

141. Can a class implement multiple interfaces in Java?

Java allows a class to inherit multiple interfaces, providing a form of  multiple inheritance without complexity and ambiguity with inheriting from multiple classes.

Example:

Java

This is a key benefit of Java interfaces, allowing decoupled and scalable architectures and supporting the separation of concerns.

142. When should you use an abstract class in Java?

Use an abstract class in Java when:

  • You would like to have default behaviour (method implementations) that your child classes will inherit.
  • Your class hierarchy has shared state (fields).
  • You must define the constructors.
  • You expect subclasses to follow a template pattern.

Abstract classes are suitable when constructing a base class with partial implementation. Abstract classes have both concrete and abstract methods, in contrast to interfaces.

143. How do you define an abstract method in Java?

An abstract method is a method with no body. It has to be declared within an interface or an abstract class and has to be overridden by the subclasses.

Syntax:

abstract class Shape {
    abstract void draw();  // No body
}

Any subclass of Shape will have to have an implementation of draw(), or be declared abstract. This will impose a behavioral contract on the subclass.

144. Is a superclass constructor called even when there is no explicit call from a subclass constructor in Java?

Yes, in Java, the superclass constructor is always invoked, even if not directly stated. The Java compiler will insert a call to the default no-argument constructor of the superclass if a subclass constructor does not include a super() call.

Java

This ensures that the object hierarchy is initialized top to bottom. In case of no no-arg constructor in the superclass, the subclass will have to call a parameterized super(…).

145. Why is exception handling important in Java?

Exception handling in Java is essential in developing robust and fault-tolerant programs. It helps to catch and process unexpected runtime exceptions without crashing the program.

Some key reasons include:

  • Decoupling error-handling logic from regular logic.
  • Enhanced readability and maintainability.
  • Failure grace, enhancing user experience.
  • Supports resource cleanup with finally or try-with-resources.

Java offers a disciplined approach to managing both checked and unchecked exceptions by means of try, catch, finally, and throw.

146. What is the purpose of the finally block in Java?

The Java finally block serves to ensure execution of cleanup code whether or not an exception was thrown and caught. It is usually applied in order to close resources such as files, sockets, or database connections.

Java

Even in case a program reaches a return statement or throws a new exception, a finally block will be executed-making it crucial for resource management in actual applications.

147. What are Generics in Java?

Java generics are a very useful feature that permits you to write reusable, type-safe code. Generics help classes, interfaces, and methods perform operations on objects of different types without knowing at compile time.

Java 5 introduced generics to avoid explicit type casting and to minimize the possibility of ClassCastException at runtime.

Example:

Java

Without generics, objects would need to be cast by hand, raising the threat of runtime errors.

148. Why do we need Generics in Java? Can you provide an example of how Generics make a program more flexible?

In Java, we use Generics to provide strong type safety, code reusability, and cleaner code. Generics enable the same class or method to be used with various data types without repetition of code.

Here’s how they promote flexibility:

Java

It avoids casting and errors and maintains code modularity and maintainability.

149. How do you declare a generic class in Java?

You declare a generic class in Java by using angle brackets (<>) with a type parameter. It informs the compiler that the class is able to work with objects of any type.

Example with Syntax:

Java

Here, T is a type parameter that is replaced with an actual data type upon object creation.

Key Points:

  • T is a type parameter, allowing the class to be type-safe and reusable.
  • This reduces the need for casting and improves code clarity and safety.

150. What are the restrictions when using generic types declared in a class in Java?

Generics enhance type safety but carry some key limitations because of type erasure in Java:

a. Cannot instantiate a generic type:

T obj = new T(); // Compile-time error

b. Cannot create arrays of generic types:

T[] arr = new T[10]; // Not allowed

c. Cannot use instanceof with generic types::

if (obj instanceof T) { } // Invalid

Cannot create static fields of generic type: Static members are shared by all instances and therefore cannot reference type parameters that are instance-dependent.

151. Can you provide an example of a generic method in Java?

Yes, a generic method enables the creation of a method that can be applied to any data type, ensuring increased reusability and type safety.

Example:

Java

Here declares the data type prior to the return type, allowing the method to be versatile to operate with different data types.

Advanced Java Interview Questions

In this section, master advanced Java interview questions to gain expertise in core Java and various important topics. These Java interview questions designed for experienced candidates so that they can enhance their problem solving skills for senior roles. Prepare these Java Developer interview questions and land a job in Top MNCs

152. Is it possible to execute Java code, even before the main method? Explain.

Yes, we can execute code even before the main method. It can be done using two ways:

  1. Static Initialisers
  2. Static Variable Initialization

Let’s understand static initialisers first. We can create a static block of code, with the code which we want to execute before the main method. Any statements within this static block of code will get executed before the main method.

Example

Java

This happens because when the JVM loads the class, it executes the static block of code first by design, even before the main method. One can have multiple static blocks, they will execute in the sequence you define them in.

Next, let’s understand Static Variable Initialization. Static variables can initialize another static method, thus making the code run before the main method, let’s understand using an example.

Java

Here, the method intellipaat is initialised by the static variable x.

153. Can you save a .java file without a name?

Yes, we can save a java file with an empty .java file name. You can compile it in the command prompt by ‘javac .java’ and run it by typing ‘java classname’. Here is an example of such a program:

Java

Once the code has been saved, you need to launch the command prompt and navigate to the file’s directory. After that, you need to write “javac .java” to compile the program and “java A” to execute it.

154. Can you define static methods inside a java interface?

Yes, a Java interface can have static methods, but we cannot override them as they’re static. So, we have to define the static methods in the interface and only perform the calling party in the class.

Below is an example of a static method in Java interface:

Java

The above interface has a static method and a default method that can be executed without affecting the class that implements it. However, there is no use in creating the static methods in the Java interface as we cannot override the function by redefining it in the main() method.

Java

155. What are annotations in Java?

  • Java annotation is a tag that symbolizes the metadata associated with class, interface, methods, fields, etc.
  • Annotations do not directly influence operations.
  • The additional information carried by annotations is utilized by Java compiler and JVM.

156. Explain the difference between Struts 1 classes and Struts 2 classes.

Struts 1 actions are singleton. So, all threads operate on the single action object, which makes it thread-unsafe.

Struts 2 actions are not singleton, and a new action object copy is created each time a new action request is made, making it thread-safe.

157. What is JAXP and JAXB?

JAXP: It stands for Java API for XML Processing. This provides a common interface for creating and using DOM, SAX, and XSLT APIs in Java regardless of which vendor’s implementation is actually being used.

JAXB: It stands for Java API for XML Binding. This standard defines a system for a script out of Java objects as XML and for creating Java objects from XML structures.

158. What is enumeration?

It is a special data type used to define a fixed set of constants for a variable. This is typically used when the values being represented are known in advance and do not change over time.

159. What’s the base class of all exception classes?

Java.Lang.throwable: It is the superclass of all exception classes, and all exception classes are derived from this base class.

160. What is the use of a vector class?

A vector class provides the ability to execute a growable array of objects. A vector proves to be very useful if you don’t know the size of the array in advance or if we need one that can change the size over the lifetime of a program.

161. How does HashMap handle collisions, and what changes were introduced in Java 8 regarding this?

In Java, a HashMap stores key-value pairs in buckets. Each key hashes to a bucket based upon applying the hashCode(), followed by a modulo operation against the array length.

Collision Handling (Pre-Java 8):

When several keys produce the same hash bucket index (a collision), Java stores entries in a singly linked list at the index. In the worst-case lookup, if there are too many collisions, the performance slows to O(n).

Java 8 Enhancement:

Java 8 added a Tree-based data structure (Red-Black Tree) to solve performance problems in cases of frequent collisions.

  • If a bucket’s linked list expands to a size larger than a certain limit (TREEIFY_THRESHOLD, typically 8), it is transformed to a tree, reducing lookup time from O(n) to O(log n).
  • This only happens if the array length (bucket array) is >= 64; resizing is better than treeification in all other cases.

Summary:

  • Before Java 8: Collision -> Linked List -> O(n)
  • Java 8+: Collision -> Tree after threshold -> O(log n)

162. Can Map interface extend the Collection interface in the Java Collections Framework?

No, The Map interface is not compatible with the Collection interface, because Map requires a key as well as a value, for example, if we want to add a key–value pair, we will use put(Object key, Object value).

There are two parameters required to add an element to a HashMap object. In the Collection interface, add (Object o) has only one parameter.

The other reasons are: Map supports valueSet, keySet, and other suitable methods that have just different views from the Collection interface.

163. Explain memory management and garbage collection in Java.

Garbage collection is Java’s automatic memory management system. It runs as a process that recovers memory from objects that will never be used again. Garbage collection is done by JVM, and it does tasks like allocating memory for objects, garbage collecting unused objects, and optimizing memory usage for performance enhancement. Various types of memory areas are used by JVM such as heap, stack, method area, and native method stacks to manage memory efficiently.

164. What are the drawbacks of Java’s garbage collection?

Although garbage collection makes memory management easier, it has a few disadvantages:

  1. Irregular Pause Durations: GC can suspend application threads (Stop-the-world events) that affect latency-sensitive applications.
  2. CPU Overhead: Garbage collection takes CPU time, notably full GC cycles.
  3. Less Control: A developer cannot exactly control when or where an object gets deallocated.
  4. Performance tuning is needed: JVM flags need to be carefully tuned to let GC behave appropriately by adjusting -Xms, -Xmx, and -XX:+UseG1GC.

Garbage collection is very powerful but requires appropriate configuration in enterprise or real-time Java applications.

165. What is the Java Memory Model and how is memory allocated?

The Java Memory Model (JMM) specifies the semantics of cooperation between threads via memory and the acceptable behaviors in concurrent programming. It is a standard that specifies reading and writing of variables by different threads within a Java Virtual Machine (JVM).

In memory allocation, this is how it usually happens in Java:

  1. Heap memory: Holds objects and class instances. It is shared by all threads.
  2. Stack memory: Holds function calls and local variables. It is thread-local.
  3. MetaSpace: Holds class metadata, static data, and method code.
  4. PC (Program Counter) Register: Identifies the current JVM instruction being processed.
  5. Native Method Stack: Manages the native (non-Java) methods.

JMM factors in visibility, atomicity, and ordering guarantees between threads. JMM is critical within synchronized blocks, volatile variables, and thread safety features such as ReentrantLock.

166. Explain the differences between minor, major, and full garbage collections in Java.

Type of GC Affected Area Frequency Pause Duration Description
Minor GC Young Generation Frequent Short Clears short-lived objects from the Eden space and moves survivors to Survivor/Old gen.
Major GC (Old GC) Old Generation Less frequent Longer Cleans up long-lived objects. May involve compaction.
Full GC Entire Heap (Young + Old) Rare Longest Cleans all memory areas, including PermGen / Metaspace. Most disruptive to application performance.

A Full GC typically includes both Minor and Major GCs and is usually triggered when memory is critically low or by System.gc() (if not disabled).

167. How can you identify major and minor garbage collections in Java?

You can identify various garbage collection modes by simply applying JVM flags and garbage collection monitoring tools.

  1. Enable GC Logs:
 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log

This will help log minor GC (e.g., “ParNew”) and major/full GC (e.g., “Tenured”, “Full GC”) events.

  1. Use Java Monitoring Tools:
  • JVisualVM
  • JConsole
  • GCViewer
  • GCEasy.io (log analyzer)
  1. Look at GC Log Patterns:
  • Minor GC: Identified by logs like GC (Allocation Failure), Young Gen, PS Scavenge, etc.
  • Major/Full GC: Appears as Full GC, CMS, or G1 Old Generation.

Real-World Tip: Getting a knowledge of GC logs is essential to performance tuning. Recurring full GCs are usually a sign that there are memory leaks and/or heap size misconfigurations.

168. What is a memory leak in Java, and how does it affect garbage collection?

In Java, a memory leak happens when objects that are no longer needed remain referenced and thus are not garbage collected. Even with automatic garbage collection in Java, memory leaks are possible if objects are kept accessible by virtue of poor programming practices.

Impact on Garbage Collection:

  • Avoids memory reclaiming by the GC.
  • Slowly increases usage of the heap, causing OutOfMemoryError.
  • Triggers degradation in performance, prolonged GC pauses, and system instability.

169. What design pattern is commonly used to implement exception handling in programming languages?

The Chain of Responsibility pattern is frequently employed when dealing with exception handling. In Java, this is seen in the propagation of exceptions upwards in the call stack such that one or more catch statements catch the error based upon its type.

In this pattern:

  • Employees are waiting in a line of handlers.
  • Every handler will determine whether to service the request or to pass it.
  • Java exception handling mirrors this by searching every catch block for a corresponding exception.

It encourages separation of concerns and makes error-handling more modular and maintainable.

170. What is the difference between synchronized blocks and ReentrantLock in Java?

Both synchronized block and ReentrantLock in Java are employed in thread synchronization, but vary in terms of flexibility, functionality, and control:

Feature synchronized ReentrantLock
Syntax Implicit monitor lock (synchronized(obj)) Explicit lock acquisition (lock.lock())
Flexibility Less flexible More flexible
Try-Lock Not available tryLock() with timeout support
Interruptibility Not interruptible Can interrupt a waiting thread
Fairness No fairness policy Can enforce fairness
Condition Variables Uses wait() / notify() Supports multiple Condition objects

Use synchronized in simple cases. Use ReentrantLock if more control is desired, e.g. in deadlock detection, try-locking, or fairness.

171. How does the Java Memory Model (JMM) ensure visibility and ordering of variables in multithreaded environments?

The Java Memory Model (JMM) provides ordering, atomicity, and visibility guarantees between threads by describing the memory interaction between threads (in the sense of shared variables). JMM addresses typical problems such as reordering and stale reads by compilers or processors.

Important concepts:

  • Visibility: Modifications done by a single thread to shared variables are not visible to others until appropriate synchronization.
  • Happens-before relationship: Guarantees memory operations to be viewed in the appropriate order.
    • start() occurs before the thread’s run.
    • Synchronized and volatile fields define happens-before relationships.
  • Volatile variables: Provide visibility without locking but not atomicity.
  • Locks: Both mutual exclusion and visibility guarantees are enforced.

The JMM renders concurrent program execution predictable and thread-safe by controlling instruction reordering and caching effects.

172. How to properly benchmark Java code using JMH?

To benchmark Java code reliably, use JMH (Java Microbenchmark Harness), a toolkit developed by the same team that maintains the JVM. JMH is generally created to prevent typical benchmarking issues such as loop unrolling, JVM warm-up effects, and dead-code elimination.

Steps to benchmark code using JMH:

  • Annotate the method to be benchmarked with @Benchmark
  • Use @Warmup, @Measurement, and @BenchmarkMode for configuration
  • Run the benchmark via a main() method or Maven plugin

Example (JMH requires setup via Maven or Gradle):

Java

Note: JMH-based code is only executabe with the help of the JMH plugin (mvn clean install; java -jar target/benchmarks.jar). They cannot be run in online IDEs such as JDoodle or Replit because of the requirements of annotation processing and build tool dependencies.

173. What are the advantages and potential pitfalls of using thread pools in Java?

Thread pools, usually established with the help of Executors, maintain a pool of reusable threads to carry out work. They enhance execution efficiency and resource usage, but if misconfigured, have hidden dangers.

Advantages:

  • Efficient use of resources: The threads are reused, minimizing thread creation cost.
  • Enhanced scalability: Supports many concurrent activities without overloading system resources.
  • Queuing of tasks and efficient execution of them.
  • Improved control: You can adjust thread limits, keep-alive intervals, and rejection policies.

Drawbacks:

  • Improper sizing: Too few threads -> underutilization; too many -> CPU thrashing or OutOfMemoryError.
  • Unbounded queues: Have the potential to cause memory issues if tasks accumulate faster than they can be processed.
  • Thread leakage: When threads block forever, threads are held busy, impacting system responsiveness.
  • Complex error handling: Demands explicit exception handling within tasks to prevent silent failures.

Use thread pools with optimal usage by configuring them using ThreadPoolExecutor and measuring the metrics in production.

174. Explain the concept of false sharing in multithreaded applications and how it affects performance.

False sharing is a performance-degrading occurrence where several threads write to variables that happen to be stored near together in memory (on the same CPU cache line) even if the threads are operating on entirely different variables.

Although logically unrelated, all the variables are allocated in a cache line, and when a thread updates a variable, the entire cache line is invalidated with:

  • Constant cache invalidation
  • Excessive memory traffic
  • Decreased CPU efficiency
  • Increased contention

Example Scenario:

Java

If x and y are in the same cache line, updates by both threads cause false sharing.

Solution:

  • Padding variables: Insert dummy fields to distinguish variables.
  • Use the @Contended annotation (Java 8+ with -XX:-RestrictContended flag) to avoid false.

Avoiding false sharing can greatly enhance multithreaded program throughput and cache efficiency.

175. What are the differences between Serial, Parallel, CMS, and G1 garbage collectors in Java?

In Java, Garbage Collectors, or GCs, are tasked with automatic memory management. Each is optimized to serve particular cases like minimal pause times or maximal throughput.

Garbage Collector Characteristics Best Use Case
Serial GC Single-threaded; stops all application threads during GC (Stop-the-World). Small applications or environments with single CPUs.
Parallel GC (Throughput Collector) Multi-threaded GC; focuses on maximizing throughput. Applications where high throughput is more important than low pause times.
CMS (Concurrent Mark-Sweep) Minimizes pause times by doing most of the work concurrently with the application. Low-latency applications requiring shorter GC pauses.
G1 (Garbage First) Region-based, concurrent collector; balances throughput and pause time with predictable performance. Large heap, server-side applications needing predictable pause times.

G1 GC has become the default collector in more recent versions of Java (Java 9+) and replaced CMS in this role, and ZGC and Shenandoah are recent low-pause collectors in subsequent releases of Java.

176. How can memory leaks occur in Java applications despite automatic garbage collection?

Java’s Garbage Collector recovers memory that’s not accessible. But memory leaks are still possible where references to unused objects are held unintentionally, precluding GC to recover memory.

Common Causes:

  • Static references: Static fields retaining objects outside their lifespan.
  • Listeners & callbacks: Failing to deregister event listeners will retain objects alive.
  • Incorrect caching: Caches that are not properly managed (such as Map without eviction) cause leaks.
  • ThreadLocal misuse: Not removing values can cause leaks, especially in thread pools.
  • Inner class references: Non-static inner classes have implicit outer class instance references.

Result: Leaked objects cause heap space consumption, leading to OutOfMemoryError.

177. What tools and techniques can be used to diagnose and resolve OutOfMemoryError in Java?

When a Java application experiences memory depletion, the JVM throws an OutOfMemoryError. It takes a mixture of monitoring, profiling, and analysis tools to correct this.

Common Tools:

  • Heap Dumps: Examine memory snapshots with tools such as:
    • Eclipse MAT (Memory Analyzer Tool)
    • JVisualVM
    • YourKit
  • Garbage Collection Logs:
    Enable with -XX:+PrintGCDetails, -Xlog:gc (Java 9+)
    • Use GCViewer or GCEasy to analyze logs.
  • Profiling Tools:
    • JProfiler, Flight Recorder, VisualVM, JConsole to monitor memory trends and object allocations.

Techniques:

  • Identify memory leaks: Find objects with a long retention in heap dumps.
  • Tune GC: Tune the heap size (-Xms, -Xmx) and GC algorithm depending upon the application.
  • Use soft/weak references: Assists in the development of memory-sensitive caches.
  • Fix code issues: Remove unused references, clean up static fields, close resources.

With the integration of these tools and methods, developers are able to identify patterns of memory use, identify leaks, and make Java applications more memory-efficient.

178. How do CompletableFuture and Future differ in handling asynchronous computations?

The Future and CompletableFuture both describe the result of a computation that happens asynchronously, but in terms of features and versatility, both are very different.

Key Differences:

Feature Future CompletableFuture
Non-blocking API No (requires .get() which blocks) Yes (supports non-blocking callbacks with .thenApply(), .thenAccept() etc.)
Chaining Not possible Supports functional-style chaining of tasks
Exception Handling Limited Built-in exception handling with .exceptionally(), .handle()
Manual Completion Not supported Supports manual completion via .complete()
Combining Futures Not supported Combine multiple async results with .thenCombine(), .allOf()

In short, apply Future to simple asynchronous operations where blocking is fine. Apply CompletableFuture to complex workflows, non-blocking sequences, and reactive programming.

179. What are method references in Java, and how do they enhance code readability?

Java method references are a concise syntax for lambda expressions that call a pre-existing method. They make the code more readable and concise.

Syntax Types:

Java

Example:

Java

Benefits:

  • Enhances readability and maintainability.
  • Reduces boilerplate code.
  • Most suitable in functional programming applications like streams and collectors.

180. How does the Optional class help in avoiding null checks, and what are its best practices?

The Optional<T> class in Java is a container object to prevent explicit null checks and avoid NullPointerException.

How It Helps:

Methods now return Optional<T> rather than null to indicate that a value could be present or not.

Java

Best Practices:

  • Use Optional as return types, in particular where a method may not yield a value.
  • Avoid the use of Optional as a field or parameter type (according to Oracle recommendations).
  • Use .orElse(), .orElseGet(), or .orElseThrow() to gracefully deal with empty values.
  • Chain operations securely with .map() or .flatMap().

Anti-Patterns to Avoid:

  • Don’t use Optional in performance-critical code (has overhead).
  • Don’t call Optional.get() without first doing Optional.isPresent() – that misses the point.

In short, Optional supports a more explicit, safer approach to dealing with missing values without relying on explicit null tests, enhancing code readability and reliability.

181. Can you implement the Singleton pattern in a thread-safe manner without using synchronization?

Yes, a thread-safe Java Singleton without explicit synchronization can be created with the Bill Pugh Singleton Design Pattern that utilizes Java Memory Model’s guarantees around class loading to ensure safe and efficient lazy initialization.

Implementation Using Static Inner Class:

Java

How it works:

  • The inner class is only loaded when the call to getInstance() occurs.
  • Class loading is guaranteed to occur only once by the JVM and is thread-safe.
  • No synchronized or double-checked locking is necessary.

This method is very efficient and guarantees lazy initialization, thread safety, and optimal performance, because that is the Singleton pattern of choice in contemporary Java.

182. What is the Open/Closed Principle, and how does it apply to Java interfaces and abstract classes?

The Open/Closed Principle (OCP) is a part of the SOLID design principles of object-oriented design. It states:

“Software entities should be open for extension but closed for modification.”

In Java, this means:

  • You can extend a class or interface without having to change its source code.
  • Interfaces and abstractions provide this by means of polymorphism.

Application in Java:

  • Interfaces permit various implementations without a change in the interface itself.
  • Abstract classes can specify a general framework yet let subclasses extend function.

Example:

Java

Here, introducing a new payment method doesn’t require modifying current code. You simply create a new class that implements the PaymentStrategy interface.

183. How does the Strategy pattern promote flexibility in algorithm selection?

The Strategy pattern encapsulates a family of algorithms, allowing them to be defined independently and made interchangeable at runtime. It permits a client to change an algorithm’s behavior at runtime without actually modifying the implementation.

Structure:

  • Context: Uses a Strategy
  • Strategy interface: Specifies a contract
  • Concrete Strategies: Various implementations of the algorithm

Example in Java:

Java

Benefits:

  • Encourages reusability and flexibility .
  • Promotes separation of concerns .
  • Ensures that new algorithms are easy to incorporate without modifying current code (compliant with Open/Closed Principle).

184. What are the differences between ConcurrentHashMap and Collections.synchronizedMap()?

Both Collections.synchronizedMap() and ConcurrentHashMap are employed to make a Map thread-safe, but with vastly different performance, granularity, and concurrency.

Feature ConcurrentHashMap Collections.synchronizedMap()
Thread Safety Fine-grained (segment-level locking) Entire map is locked (coarse-grained)
Concurrency High concurrency Low concurrency
Null Keys/Values Not allowed Allows one null key and multiple null values
Performance Better under high thread contention Poor performance under load
Iteration Weakly consistent Must use external synchronization
Introduced In Java 5 (java.util.concurrent) Legacy approach using Collections

Use ConcurrentHashMap in contemporary concurrent applications. It supports non-blocking reads, lock striping, and increased scalability.

185. Explain the internal workings of LinkedHashMap and its use cases.

A LinkedHashMap is a subclass of HashMap that has a doubly-linked list that goes through all of its entries. This feature causes it to retain insertion order or access order, depending upon the way it is set.

Constructors:

LinkedHashMap<K, V> map = new LinkedHashMap<>(16, 0.75f, true);
  • The third parameter, accessOrder, controls whether iteration order is by insertion (false) or by access (true).

LinkedHashMap with LRU Cache Example:

Java

Internal Working of LinkedHashMap

  • Internally extends HashMap.
  • Maintains a doubly-linked list connecting all entries in insertion or access order.
  • When accessOrder = true, every access (get or put) moves the accessed entry to the end of the list.
  • Has a customizable method removeEldestEntry() that helps control eviction – ideal for implementing caches like Least Recently Used (LRU).

Use Cases:

  • LRU Cache Implementation – with access order and overridden removeEldestEntry().
  • Order-sensitive operations – e.g. displaying items in the same order that they were added.
  • Readable caching layer – where the ordering is significant in UI or output.

LinkedHashMap is suitable where ordering is needed with predictable time for putting and getting, similar to that of HashMap.

186. What is reflection in Java, and what are its potential security implications?

Java Reflection is a very powerful feature that permits programs to examine and change the runtime characteristics of classes, methods, fields, and constructors, even private members.

It belongs to the java.lang.reflect package and is usually employed in:

  • Frameworks such as Spring and Hibernate
  • Dependency Injection
  • Annotation processing
  • Dynamic proxies

Example:

Java

Security Implications:

  • Bypassing access control (private field/method access).
  • Can lead to data leaks or unauthorized operations .
  • Can cause breaking encapsulation by breaching Java’s object-oriented concepts.
  • Slower and more error-prone is reflection.

Best practice: Restrict reflection with a SecurityManager or with module boundaries (JPMS) in Java 9+.

187. What are soft, weak, and phantom references in Java, and how do they differ?

Java includes three categories of non-strong references in the java.lang.ref package to facilitate flexible memory handling apart from null.

Reference Type Collected When Use Case
SoftReference Memory is low Caching (removed only when JVM is low on memory)
WeakReference On next GC cycle Mapping with weak keys (e.g., WeakHashMap)
PhantomReference After finalization, before GC reclaim Cleanup before memory deallocation

188. How can you prevent deadlocks in a multithreaded Java application?

Deadlock happens when two or more threads await each other to free a lock, and neither moves.

Strategies to Prevent Deadlock:

  1. Consistent Lock Ordering: Always lock in the same order.
Java
  1. Try-Lock with Timeout: Use ReentrantLock.tryLock() with a timeout to prevent waiting indefinitely.
Java
  1. Use Lock Timeout or Deadlock Detection Tools: Deadlocks can be detected at runtime by monitoring tools such as VisualVM or jstack.
  2. Minimize Lock Scope: Decrease the amount of code within synchronized regions to reduce lock contention..
  3. Avoid Nested Locks: Organize logic to prevent nested or cyclic lock acquisition.

Deadlocks necessitate a focused design, disciplined lock strategy, and occasionally taking advantage of the higher-level concurrency utilities provided by the java.util.concurrent package.

189. How does the ForkJoinPool work in Java’s concurrency framework?

The ForkJoinPool is an extended implementation of Java’s ExecutorService that is particularly suited for running tasks in parallel and for efficiently handling parallelizable, recursive tasks. It is part of the package java.util.concurrent and is particularly tuned to operate with work-stealing algorithms, enhancing utilization in multi-processor environments.

Key Concepts:

  • Work Stealing: The pool’s threads can “steal” a job from other threads when they are idle to provide load balance and increase performance.
  • Forking and Joining: The tasks are decomposed into smaller tasks recursively (forked) and then joined to yield results.
  • RecursiveTask/RecursiveAction: RecursiveTask has a return value and RecursiveAction does not.

Usage Example:

Java

190. Explain the double-check locking idiom for Singleton classes.

Double-checked locking is a method utilized for safe lazy initialization of a Singleton class, without synchronization overhead on every access to the Singleton.

Steps:

  1. Verify that the Singleton instance is already created (initial check).
  2. Otherwise, synchronize only that block of code that instantiates an instance, once to prevent performance premiums.
  3. Double-check that the instance is not null in the synchronized section (second check). This guarantees only a single thread may construct the instance.

Code Example:

Java

Why Double-Check Locking?

  • Performance: Evades synchronization overhead once the instance has been initialized.
  • Thread-safety: Ensures only a single instance of the Singleton class is created within a multi-threading environment.

191. What is the difference between phantom references and weak references?

Weak references and phantom references are part of Java’s framework for dealing with references (java.lang.ref package), enabling developers to use objects without keeping them out of the garbage collector.

Weak Reference:

  • Weak reference permits an object to become eligible for garbage collection when it is weakly reachable (i.e., there are no strong references).
  • It is usually employed to cache or canonicalize objects for when we wish to have that object reclaimed when there is a need for memory.

Example:

WeakReference weakRef = new WeakReference<>(new MyObject());
  • Garbage Collection: When there are no strong references to an object, it will get collected even when there is a weak reference.

Phantom Reference:

  • Phantom references are superior to weak references. Phantom references permit a program to receive notification when an object is going to get garbage collected.
  • Phantom references cannot directly be dereferenced, and they are employed with a reference queue to execute cleanup operations before an object is reclaimed.

Example:

PhantomReference phantomRef = new PhantomReference<>(new MyObject(), new ReferenceQueue<>());
  • Garbage Collection: Access to this object cannot be made directly via this phantom reference. It will instead get included in the reference queue once ready for garbage collection.
  • Use Case: Usually utilized to finalize, tidy up resources, or manage memory use.

Differences:

  • Weak references provide access to the object (if it has not been garbage collected), while phantom references do not.
  • Phantom references are utilized for pre-finalization cleanup, while weak references are typically utilized for caching or temporary storing.

192. How does the Java module system (JPMS) improve encapsulation?

The Java Platform Module System (JPMS), introduced in Java 9, permits developers to specify modules and regulate visibility and accessibility of code within modules. It aids in a better encapsulation compared to earlier versions of Java in that it restricts what classes and packages may be accessed by other modules.

Example of a Module:

module com.myapp {
    requires java.sql;  // Declares dependency on java.sql module
    exports com.myapp.api; // Exports only the API package
}

Benefits of Encapsulation:

  • Internal implementation hiding: A module exports only certain packages, with other packages or modules having no way to directly access internal classes unless permitted explicitly.
  • Dependency control: You can avoid unwanted dependencies and have more robust separation of components.
  • Enhanced security: By regulating access to critical packages, you can reduce risks related to code exposure.

Example:

module my.module {
    exports com.example.publicAPI;  // Only this package is accessible externally
    // other internal packages remain hidden from external access
}

193. What is the Reactive Streams API in Java 9+?

The Reactive Streams API was added to Java 9 as part of the package java.util.concurrent.Flow. The API defines a standardized means to process asynchronous streams with backpressure, making systems responsive and scalable, especially when processing high-volume data.

Key Concepts:

  • Backpressure: Reactive Streams prevent the system from getting inundated with a high volume of data. The production rate is slowed down or stopped temporarily, depending on when and how much data the consumer is able to process.
  • Publisher, Subscriber, Subscription, Processor: The four Reactive Streams API’s most fundamental interfaces.
    • Publisher: Generates data items (i.e., data source).
    • Subscriber: Consumes data items (for example, data handler).
    • Subscription: Refers to the relationship between Subscriber and Publisher, controlling data flow.
    • Processor: Acts as a combination of Subscriber and Publisher, it receives and forwards data.

Example:

Java

194. How do you use MethodHandle for dynamic method invocation?

MethodHandle is a member of package java.lang.invoke and is a flexible, safe mechanism to invoke methods dynamically with no reflection overhead. It is more efficient than Java’s reflection when invoking methods and is utilized in high-performance applications such as dynamic proxies, language interpreters, and custom class loading..

Example of Using MethodHandle:

Java

195. Explain the escape analysis optimization in the JVM.

Escape analysis is a JVM optimization technique that examines whether an object escapes its method or execution thread. In case an object does not escape (i.e., remains used within a method or thread), then JVM may use techniques such as stack allocation or scalar replacement, whose impact on eliminating memory overhead as well as improving performance can be very high.

Key Concepts:

  • Escape: An object “escapes” when it is reachable outside the method or thread that produces it (e.g., in a field or as a parameter to a different thread).
  • Stack Allocation: When an object does not escape, it is possible for the JVM to allocate it on the stack rather than on the heap, thus eliminating garbage collection overhead.
  • Scalar Replacement: In case an object is made of primitive types and doesn’t escape, the JVM may replace the object with its fields as a means to bypass object creation altogether.

Example:

Java

JVM Implementation:

  • Escape analysis can be enabled or configured via JVM flags, such as -XX:+DoEscapeAnalysis and -XX:+EliminateAllocations.

196. What is biased locking in JVM thread synchronization?

Biased locking is an optimization mechanism employed by the JVM to minimize the costs involved in acquiring and releasing locks in single-thread contexts. The optimization is made to synchronization mechanisms in order to prevent contention that would otherwise take place when several threads constantly acquire the same lock.

How It Works:

  1. The thread obtains the lock.
  2. The JVM flags the lock as biased in favor of the acquiring thread.
  3. The lock is acquired rapidly in later accesses with no synchronization overhead.
  4. If some other thread attempts to obtain the lock, then this bias is removed and the lock reverts to normal synchronization.

Example:

  • The lock is biased to a thread when there is no competing thread for it, thus there is no need for the thread to keep checking for contention on a lock.
  • If contention is detected, the JVM cancels the bias, and the lock will need more classic synchronization.

197. How do you implement a custom ClassLoader in Java?

A Custom ClassLoader is a class that loads classes into the JVM. Class loading is by default implemented as a ClassLoader hierarchy, but for certain situations, like when you implement custom dynamic loading of code, you may have to create a custom class loader.

In most Custom ClassLoaders, the findClass() method is overridden to specify how a class is to be loaded.

Steps to Implement a Custom ClassLoader:

  1. Extend the ClassLoader class.
  2. Override the findClass() method, which loads the class by reading it from the file system, database, or over the network.
  3. Optionally override loadClass() to alter the default class-loading behavior (e.g., delegate to some other ClassLoader).

Example:

Java

198. What are Project Loom and virtual threads in Java?

Project Loom is an active Java platform project whose objective is to make Java concurrency easier by adding support for virtual threads. Virtual threads provide better management of concurrency, particularly in use cases involving very concurrent applications that have a high need for many threads with low memory use.

Key Concepts of Project Loom and Virtual Threads:

  • Virtual Threads are lightweight threads managed by the JVM instead of by the operating system. They are inexpensive to create and use much less memory than platform threads.
  • Fiber-based Model: The idea behind virtual threads is rooted in a fiber-based concurrency model where multiple virtual threads are multiplexed over a lower number of OS threads. This means you are able to execute thousands or even millions of virtual threads simultaneously with little overhead.
  • Simplified concurrency: The addition of virtual threads simplifies concurrency because it eliminates having to manage a complicated thread pool or explicitly scheduling tasks.

Benefits of Virtual Threads:

  1. Scalability: Virtual threads enable a JVM to run thousands or millions of concurrent tasks because there is very little overhead with each virtual thread.
  2. Simpler Code: Virtual threads make it easier for developers to implement concurrent programming in a linear and sequential fashion, like with blocking I/O operations. This makes the code easier and less complicated than callbacks or asynchronous programming.
  3. Efficient use of resources: Because virtual threads are much lighter in weight than platform threads, they use much less CPU resources and memory.

Example of Virtual Thread Usage (Preview in Java 19+):

Java 19 and later versions provide virtual threads as a preview feature in Project Loom. Virtual threads may be created and manipulated with the use of the java.util.concurrent.Executors framework, just as you would manipulate platform threads with thread pools.

Java

199. How to Crack a Java Interview?

You can crack the Java Interview by mastering core Java concepts, solving numbers of coding challenges and preparing not only for the technical round of interview but also for the behavioural round. Here are the few steps you can follow for succeeding in your next Java Interview:

  • Learn Core Java: Cover the important concepts like OOP, exception handling, collections, multithreading, and Java 8+ such as Streams and Lambdas.
  • Practice Coding: Solve different questions of Java Interview on different coding websites like Leetcode, codechef, HackerRank etc.
  • Learn Frameworks: If you are targeting any particular domain like backend development or full stack development, it is very important to learn frameworks like Spring, SpringBoot, Hibernate, JDBC.
  • System Design and Patterns: You also need to know the design patterns like Singleton, Factory so that you are able to discuss low-level design and real-world architecture scenarios with the interviewers.
  • Mock Interviews and Resume: Participate as much as you can in mock interviews and focus on creating a good resume to highlight Java projects, skills, and certifications.

Always be consistent, practice these Java interview questions and answers and follow all the above mentioned steps so that you can crack any Java developer interview for fresher to senior level roles

Conclusion

We believe that this extensive list of Java interview questions and answers has aided your preparation in boosting your chances of cracking Java developer interviews in 2025. Whether a fresher trying to get your first job or a Java professional with years of experience targeting top notch tech firms, these fundamental Java questions, combined with topics such as multithreading, collections framework, exception handling, and various Java features, will make you stand apart.

By practicing these popular Java technical interview questions, you’ll be more confident in addressing both coding interviews and theory questions. Continue practicing the examples presented here, grasp the concepts entirely, and don’t miss to stay current with the new Java updates like Java 21, Java 22 and Java 23 and their new features.

If your intention is to crack your next Java interview, bookmark this guide, and practice writing code for every concept. You can also check out our Java Course so that your dream Java job can only be one well-prepared interview away!

Java Interview Questions and Answers – FAQs

Frequently Asked Questions
1. What are the common job roles for a Java developer?

Java developers are generally hired for different job roles like Backend Developer, Full Stack Java Developer, Java Software Engineer, Android Developer (Java-based), and Java Microservices Developer. They are also hired for specialized roles like Java Architect and DevOps with Java expertise.

2. How many rounds are there in a typical Java interview process?

Most Java developer interviews involve 3–5 rounds, including:

  • Online coding assessment
  • Technical interview (Core Java, OOPs, Multithreading, etc.)
  • System design or architecture round (for experienced roles)
  • Managerial or HR round
  • Optional: Live project discussion or pair programming
3. Which companies hire Java developers actively?ccc

Companies like TCS, Infosys, Wipro, Accenture, Capgemini, Tech Mahindra etc, typically hire skilled Java Developers in India. Globally, companies like Amazon, Google, JPMorgan Chase, IBM, and Oracle also actively hire Java talent for scalable creating their backend and enterprise solutions.

4. What is the average salary of a Java developer in India and abroad?

In India, Java developer salaries range from ₹4 LPA to ₹20+ LPA depending on experience and tech stack.

  • Freshers: ₹3–6 LPA
  • Mid-level (3–6 yrs): ₹8–15 LPA
  • Senior roles (7+ yrs): ₹18–30+ LPA

In the US, the average salary ranges between $90,000 to $130,000+ annually.

5. What skills should I learn alongside Java to boost my career?

To increase your chances of getting hired and growing in your role, learn:

  • Spring Boot, Hibernate, REST APIs
  • Microservices Architecture
  • SQL & NoSQL Databases
  • CI/CD tools, Docker, Kubernetes
  • Cloud platforms (AWS, GCP, Azure)

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.