Java – Interview Questions
Explore a curated collection of Java interview questions covering a wide range of topics and difficulty levels—from beginner to advanced. Whether you’re just starting out or preparing for senior-level interviews, this page helps you practice effectively with clear questions, concise answers, and in-depth explanations.
Use the filter to quickly find questions by level and focus on the areas that matter most to you. Each question is designed to reinforce key concepts, improve your problem-solving skills, and prepare you for real-world technical interviews.
Start practicing, test your knowledge, and get interview-ready with structured, high-quality Java questions.
Why is ZonedDateTime preferred over LocalDateTime in global apps?
ZonedDateTime is preferred in global applications because it includes the time zone, making timestamps unambiguous across regions. LocalDateTime has no zone information and can be ambiguous.
What is ZonedDateTime?
ZonedDateTime represents a date and time with a full time zone. It accounts for zone rules such as daylight saving time.
Why is Instant useful in distributed systems?
Instant is useful in distributed systems because it provides a single time-zone-independent reference point in UTC, which makes timestamps consistent across servers and regions.
What is Instant in Java?
Instant represents a specific moment on the UTC timeline. It is ideal for storing machine-readable timestamps.
When should you use Duration instead of Period?
Use Duration when you need precise time-based measurements, such as hours, minutes, or seconds. Use Period for date-based differences like years, months, and days.
What is Duration in Java?
Duration represents a time-based amount, such as a number of seconds or nanoseconds. It is useful for measuring elapsed time between two instants or date-times.
What is the difference between Period and Duration?
Period is date-based and measures time in years, months, and days. Duration is time-based and measures time in hours, minutes, seconds, and nanoseconds.
What is Period in Java?
Period represents a date-based amount of time in years, months, and days. It is mainly used with date values such as LocalDate.
What is the difference between OffsetDateTime and ZonedDateTime?
OffsetDateTime stores a date-time with a fixed UTC offset, while ZonedDateTime stores a date-time with a full time zone, including daylight saving rules.
What is the difference between BigDecimal equals and compareTo?
BigDecimal.equals() checks both the value and the scale, so 2.0 and 2.00 are not equal. compareTo() compares only the numeric value, so those two values are considered equal.
Can you overload methods by return type only?
No, you cannot overload methods by return type only. The parameter list must be different for overloading to work.
What is method overloading?
Method overloading means defining multiple methods with the same name but different parameter lists. It provides multiple ways to perform a similar operation.
What is dynamic method dispatch?
Dynamic method dispatch is the mechanism by which Java chooses the overridden method to execute at runtime based on the actual type of the object, not the reference type.
What is the difference between compile-time and runtime polymorphism?
Compile-time polymorphism is usually achieved through method overloading, where the method to call is determined at compile time. Runtime polymorphism is achieved through method overriding, where the method is chosen based on the actual object type at runtime.
What is encapsulation in Java?
Encapsulation is the practice of bundling data and behavior together and restricting direct access to internal state. It is typically achieved using private fields and controlled access through methods.
What is the difference between a class and an object?
A class is a blueprint that defines state and behavior. An object is an instance of that class created at runtime.
Can enums have methods and constructors?
Yes, enums can have methods, fields, and constructors. Their constructors are always private or package-private and are used internally to initialize enum constants.
What is an enum in Java?
An enum is a special type used to define a fixed set of constants. It is useful when a variable should have one value from a predefined list.
What are the limitations of var?
var can only be used for local variables, loop variables, and try-with-resources variables. It cannot be used for fields, method parameters, or return types, and it requires an initializer.
What is the var keyword in Java and when should you use it?
var enables local variable type inference, meaning the compiler infers the type from the initializer. Use it when the type is obvious from the right-hand side and it improves readability.