
Java remains one of the most in-demand programming languages for backend development, enterprise applications, and automation. Whether you’re a fresher or an experienced developer, preparing for interviews with the Top 50 Core Java Interview Questions can give you a serious edge. Below, we list these questions with well-detailed answers, examples, and real-world use cases.
Java Interview Questions
1. What is Java?
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems in 1995. It follows the WORA (Write Once, Run Anywhere) principle, meaning compiled Java code can run on all platforms that support Java without recompilation.
- Platform-independent due to JVM
- Supports OOP concepts like inheritance, polymorphism, encapsulation, and abstraction
- Used in web, enterprise, mobile, and desktop applications
2. What is JVM, JRE, and JDK?
- JDK (Java Development Kit):Contains tools to develop Java programs, including JRE and compilers.
- JRE (Java Runtime Environment):Provides libraries and JVM to run Java programs.
- JVM (Java Virtual Machine):Executes Java bytecode and makes Java platform-independent.
3. Explain the OOP principles in Java.
Java supports 4 core OOP principles:
- Encapsulation – Binding data and code
- Abstraction – Hiding implementation details
- Inheritance – Code reuse through parent-child relationships
- Polymorphism – One name, multiple forms (method overloading/overriding)
4. What is the difference between == and .equals()?
==
checks reference equality.equals()
checks content equality
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true
5. What is a constructor in Java?
A constructor is a special method used to initialize objects.
- Has the same name as the class
- No return type
public class Book {
String title;
public Book(String t) {
title = t;
}
}
6. What is method overloading and overriding?
- Overloading – Same method name, different parameters (Compile-time)
- Overriding – Same method name in subclass (Run-time)
// Overloading
int add(int a, int b) {...}
double add(double a, double b) {...}
// Overriding
class A {
void show() {}
}
class B extends A {
void show() {}
}
7. Difference between ArrayList and LinkedList?
Feature | ArrayList | LinkedList |
---|---|---|
Storage | Dynamic array | Doubly linked list |
Access time | Faster (O(1)) | Slower (O(n)) |
Insert/Delete | Slower | Faster |
8. What is the difference between interface and abstract class?
- Interface: All methods are abstract (Java 8+ allows default methods)
- Abstract Class: Can have abstract and concrete methods
interface Animal {
void sound();
}
abstract class Bird {
abstract void fly();
void sleep() {
System.out.println("Sleeping");
}
}
9. What are checked and unchecked exceptions?
- Checked Exceptions: Checked at compile time (e.g., IOException)
- Unchecked Exceptions: Checked at runtime (e.g., NullPointerException)
10. What is multithreading in Java?
Multithreading allows concurrent execution of two or more threads for maximum CPU utilization.
- Create thread by:
- Extending
Thread
class - Implementing
Runnable
interface
- Extending
class Demo extends Thread {
public void run() {
System.out.println("Thread running");
}
}
11. What are access modifiers in Java?
Access modifiers in Java define the scope and visibility of classes, methods, constructors, and variables. They help implement encapsulation and control how the members of a class can be accessed from other classes.
1. private
- Scope: Within the same class only.
- Usage: Used to restrict direct access from outside the class.
- Best Practice: Encapsulate fields and expose them via public getters/setters.
2. default (no modifier)
- Scope: Accessible only within the same package.
- Keyword: There’s no keyword; simply omit the modifier.
- Use Case: When you want to expose members to other classes in the same package.
3. protected
- Scope: Accessible within the same package and by subclasses (even if in different packages).
- Use Case: Common in inheritance.
4. public
- Scope: Accessible from anywhere (any class, any package).
- Use Case: API methods, utility classes, and entry points like the
main
method.
12. What is a static keyword?
The static
keyword in Java is a non-access modifier used primarily for memory management. It can be applied to variables, methods, blocks, and nested classes. When a member is declared static
, it belongs to the class itself rather than an instance of the class.
13. Difference between HashMap and Hashtable?
Both HashMap
and Hashtable
are part of Java’s Map interface, used to store key-value pairs. However, they differ in synchronization, performance, and usage
HashMap
is a non-synchronized implementation of the Map interface that allows null keys and values.Hashtable
is a legacy, synchronized class that does not allow null keys or values.
14. What is final, finally, and finalize()?
- The
final
keyword is used to declare constants, prevent method overriding, and inheritance. - The
finally
block is used with try-catch to execute code regardless of whether an exception occurs or not. - The
finalize()
method is called by the Garbage Collector (GC) before destroying an object.
15. What are Lambda Expressions in Java 8?
Lambda Expressions are used to call the method of a functional interface. They provide anonymous implementation to the abstract method of functional interface.
List list = Arrays.asList("A", "B", "C");
list.forEach(s -> System.out.println(s));
16. Functional Interface?
- An interface with one abstract method (
@FunctionalInterface
). Read More on Functional Interface.
17. Explain the Stream API.
- Allows processing collections in a functional style. Read More on Stream API in Java 8.
list.stream().filter(s -> s.startsWith("A")).collect(Collectors.toList());
18. Optional Class in Java 8?
- Avoids
NullPointerException
Optional name = Optional.ofNullable("Java");
name.ifPresent(System.out::println);
19. Difference between equals() and hashCode()?
equals()
compares valueshashCode()
helps in hashing
Top 50 Core Java Interview Questions
20. What is the purpose of the transient keyword?
- Prevents serialization of a variable
21. What is the difference between throw and throws in Java?
throw
is used to explicitly throw an exception from a method or block.throws
is used in a method signature to declare possible exceptions that might occur.
public void checkAge(int age) throws IllegalArgumentException {
if(age < 18) {
throw new IllegalArgumentException("Not eligible");
}
}
22. What is synchronization in Java?
Synchronization ensures that only one thread can access a block of code or method at a time, preventing race conditions in multithreading.
- Used with synchronized keyword
- Can be applied to methods or blocks
public synchronized void increment() {
counter++;
}
23. What is a deadlock in Java?
Deadlock occurs when two or more threads are blocked forever, each waiting on the other.
Example Scenario:
- Thread A holds Lock 1 and waits for Lock 2
- Thread B holds Lock 2 and waits for Lock 1
Avoid deadlock by:
- Using a consistent locking order
- Avoiding unnecessary locks
24. What is garbage collection in Java?
Garbage collection automatically removes unused objects from memory.
- Performed by JVM
- Uses algorithms like Mark and Sweep, Generational GC
System.gc(); // Suggests JVM to perform garbage collection
25. What is the use of the ‘super’ keyword?
- Refers to the immediate parent class
- Used to call parent constructors or overridden methods
class A {
void display() { System.out.println("A"); }
}
class B extends A {
void display() {
super.display(); // calls A's display
System.out.println("B");
}
}
26. What is method hiding in Java?
Method hiding occurs when a static method in a subclass has the same signature as in the superclass. It does not override the method.
class A {
static void show() { System.out.println("A"); }
}
class B extends A {
static void show() { System.out.println("B"); }
}
27. Difference between String, StringBuilder, and StringBuffer?
Feature | String | StringBuilder | StringBuffer |
---|---|---|---|
Mutability | Immutable | Mutable | Mutable |
Thread-safe | No | No | Yes |
Performance | Slow | Fast | Slower |
28. What is the use of the ‘this’ keyword?
- Refers to the current instance of the class
- Resolves naming conflicts in constructors/methods
public class Person {
String name;
Person(String name) {
this.name = name;
}
}
29. Can you override a static method in Java?
No, static methods cannot be overridden, they are hidden (method hiding). They belong to the class, not instances.
Top 50
30. What is a Singleton class? How to implement it in Java?
Singleton ensures that a class has only one instance.
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
31. What is Java Reflection?
Reflection is an API used to inspect and modify runtime behavior of classes, methods, and fields.
Class> clazz = Class.forName("java.lang.String");
Method[] methods = clazz.getDeclaredMethods();
32. What is the difference between compile-time and run-time polymorphism?
- Compile-time: Method overloading
- Run-time: Method overriding
33. What is an enum in Java?
Enums are used to define a set of named constants.
enum Day { MONDAY, TUESDAY, SUNDAY }
34. What is the difference between final, static, and constant variables?
final
: Value can’t be changedstatic
: Belongs to class- Constant:
static final
+UPPERCASE_NAMING
public static final int MAX_USERS = 100;
35. What is autoboxing and unboxing in Java?
- Autoboxing: Primitive to wrapper
- Unboxing: Wrapper to primitive
Integer a = 5; // Autoboxing
int b = a; // Unboxing
36. What is the volatile keyword?
Ensures visibility of changes to variables across threads.
private volatile boolean flag = true;
37. Explain exception hierarchy in Java.
- Throwable
- Exception
- Checked (IOException)
- Unchecked (RuntimeException)
- Error
- Exception
38. Difference between abstract class and interface in Java 8?
- Interfaces can have default and static methods
- Abstract classes can have constructors and fields
39. What is a marker interface?
An interface with no methods, used to signal metadata (e.g., Serializable
)
40. Explain the use of Java annotations.
Annotations provide metadata to classes, methods, and variables.
@Override
public void run() {}
41. What is dependency injection in Java?
Design pattern that allows the removal of hard-coded dependencies.
- Used extensively in Spring
42. What are functional interfaces?
Interface with only one abstract method (SAM). Read More on Functional Interface in Java 8
@FunctionalInterface
interface Operation {
int perform(int a, int b);
}
43. What is the default method in interfaces?
Allows interfaces to have method implementations.
interface Vehicle {
default void honk() {
System.out.println("Beep");
}
}
44. What is the diamond problem in Java?
Occurs due to multiple inheritance, resolved via interfaces.
45. What is method reference in Java 8?
Shorthand for a lambda expression.
list.forEach(System.out::println);
46. Explain try-with-resources.
Used to automatically close resources.
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
System.out.println(br.readLine());
}
47. What is the Optional class in Java 8?
Optional Class is used to prevents NullPointerExceptio
n in you application. Read More on Optional Class in Java 8.
Optional name = Optional.of("Java");
name.ifPresent(System.out::println);
48. What are the benefits of Java 17 over Java 8?
- Sealed classes
- Pattern matching
- Switch expressions
- Enhanced performance and security
49. What is a record in Java 16+?
Record is a concise way to create immutable data classes.
record User(String name, int age) {}
50. How do you handle memory leaks in Java?
- Use profilers like JVisualVM
- Dereference unused objects
- Close resources
- Avoid static collections for temporary data
Final Thoughts on the Top 50 Core Java Interview Questions
The Top 50 Core Java Interview Questions cover core, advanced, and Java 8+ concepts. Preparing these thoroughly can help you crack technical interviews confidently. Download Java.
- Software Development Engineer in Test (SDET): A Comprehensive Guide
- Basic Java Coding Questions 2025 Updated List
- Interview Java Coding Questions: 2025 Updated
- Java Coding Questions: 25 Essential Java 8 Programming Questions and Answers
- Top 50 Java Coding Interview Questions (With Detailed Explanations)