Skip to main content

Top 20 Java Interview Questions and Answers for Freshers (2025 Edition)

Top 20 Java Interview Questions and Answers for Freshers (2025 Edition)



Preparing for your first Java interview can feel overwhelming. To help you, we’ve compiled the top 20 most frequently asked Java interview questions with expanded answers. These cover core concepts and practical knowledge that every fresher should be confident about.


Part 1: Core Java Basics

1. What is Java?

Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now owned by Oracle). Its key philosophy is “write once, run anywhere,” meaning code compiled into bytecode can run on any system that has the Java Virtual Machine (JVM). Java is widely used for web applications, enterprise systems, Android development, and cloud solutions because of its scalability, security, and community support.

2. What are the main features of Java?

Java provides several powerful features:

  • Platform Independence: Programs run across operating systems without recompilation.

  • Object-Oriented: Everything is treated as an object, making code modular and reusable.

  • Robust & Secure: Strong memory management and security checks.

  • Automatic Garbage Collection: Frees unused memory.

  • Multithreaded: Supports concurrent execution of tasks.
    These features make Java suitable for building reliable, secure, and scalable applications.

3. What is JVM, JRE, and JDK?

  • JVM (Java Virtual Machine): Executes Java bytecode and provides a runtime environment.

  • JRE (Java Runtime Environment): Includes JVM plus libraries and components required to run Java applications.

  • JDK (Java Development Kit): Includes JRE along with development tools like compiler (javac), debugger, and documentation generator.
    In short, JDK = JRE + development tools.

4. Difference between JDK 8 and JDK 17?

JDK 8 (2014) introduced major changes like Lambda Expressions, Stream API, and new Date-Time API. JDK 17 (2021) is a long-term support (LTS) version with advanced features such as Sealed Classes, Pattern Matching for instanceof, Switch Expressions, Text Blocks, and strong performance optimizations. JDK 17 also removed outdated modules, making Java leaner and more efficient.

5. What is the difference between == and .equals() in Java?

  • == operator: Compares memory references (whether two variables point to the same object).

  • .equals() method: Compares the actual content or value of objects.
    For example, two different String objects with the same text will be equal using .equals() but not necessarily with ==. Understanding this difference is crucial in avoiding logical errors during comparisons.

6. Explain OOPS concepts in Java.

Java’s object-oriented features revolve around four pillars:

  • Encapsulation: Bundling variables and methods into a class while restricting direct access.

  • Inheritance: Allowing one class to inherit features from another, promoting reusability.

  • Polymorphism: A method or object behaving differently depending on context (compile-time and runtime).

  • Abstraction: Hiding implementation details and exposing only functionality.
    Together, these make Java flexible, reusable, and easier to maintain.

7. What is a constructor?

A constructor is a special method used to initialize objects. It has the same name as the class and does not have a return type. Constructors can be default (no arguments) or parameterized (accept values for initialization). Unlike regular methods, they are called automatically when an object is created. Example:

class Student {
   String name;
   Student(String n) {
      name = n;
   }
}

Here, new Student("John") automatically calls the constructor.

8. Difference between method overloading and overriding.

  • Overloading: Defining multiple methods with the same name but different parameter lists. It is resolved at compile-time (static polymorphism).

  • Overriding: Redefining a method from the parent class in the child class with the same signature. It is resolved at runtime (dynamic polymorphism).
    Overloading gives flexibility, while overriding provides specific implementations in subclasses.

9. What are access modifiers in Java?

Java provides four access modifiers:

  • public: Accessible from anywhere.

  • protected: Accessible within the package and subclasses.

  • default (no modifier): Accessible within the package only.

  • private: Accessible within the same class.
    They control visibility and maintain security of class members.

10. What is the difference between ArrayList and LinkedList?

  • ArrayList: Based on a dynamic array. Fast for random access but slower for insertions/deletions (except at the end).

  • LinkedList: Based on a doubly linked list. Efficient for insertions and deletions but slower for searching.
    Choice depends on the use case: frequent access → ArrayList, frequent modifications → LinkedList.


Part 2: Advanced Java Concepts

11. What is the difference between HashMap and Hashtable?

HashMap is non-synchronized, allows one null key and multiple null values, and is faster. Hashtable is synchronized, does not allow null keys/values, and is considered legacy. For multi-threaded applications, use ConcurrentHashMap instead.

12. Explain final, finally, and finalize.

  • final: Used for constants, preventing method overriding, or inheritance.

  • finally: Block that executes regardless of exception handling outcome.

  • finalize(): Method called by Garbage Collector before destroying an object.
    These keywords help in code safety, resource cleanup, and preventing unintended changes.

13. What is multithreading in Java?

Multithreading is executing multiple threads concurrently to maximize CPU usage. Threads can be created using the Thread class or Runnable interface. Benefits include better resource utilization and responsiveness, but it also introduces complexity with synchronization and deadlocks.

14. Difference between throw and throws.

  • throw: Used inside methods to explicitly throw an exception. Example: throw new IOException();

  • throws: Declares exceptions in the method signature that may be thrown. Example: void readFile() throws IOException.
    throw is for generating exceptions, while throws is for declaring them.

15. What are checked and unchecked exceptions?

  • Checked Exceptions: Checked at compile-time, e.g., IOException, SQLException. Must be handled with try-catch or declared with throws.

  • Unchecked Exceptions: Occur at runtime, e.g., NullPointerException, ArithmeticException. Usually caused by programming errors.

16. What is garbage collection?

Garbage collection is the process of automatic memory management where the JVM frees memory by removing unused objects. Developers do not explicitly delete objects, which prevents memory leaks. Tools like System.gc() can request garbage collection, but JVM decides the timing.

17. Explain static keyword.

The static keyword makes variables and methods belong to the class rather than objects.

  • Static variables: Shared among all instances.

  • Static methods: Called without creating an object.

  • Static blocks: Executed once when the class is loaded.
    Static features reduce memory usage and support utility methods.

18. What are abstract classes and interfaces?

  • Abstract Class: Can have both abstract methods (no body) and concrete methods. Supports constructors and state (variables).

  • Interface: Defines a contract with abstract methods (till Java 7), and from Java 8, can include default and static methods. Interfaces are used for multiple inheritance of behavior.

19. Difference between String, StringBuilder, and StringBuffer.

  • String: Immutable, every modification creates a new object.

  • StringBuilder: Mutable, faster, but not thread-safe.

  • StringBuffer: Mutable, thread-safe, slower due to synchronization.
    Choice depends on mutability and thread-safety requirements.

20. What is the difference between process and thread?

  • Process: Independent execution unit with its own memory space. Heavyweight.

  • Thread: Lightweight sub-process within a process, shares memory and resources.
    Threads are more efficient for multitasking within a single application.


Conclusion

These 20 Java interview questions cover essential basics and advanced concepts. By preparing these thoroughly, freshers can answer confidently and stand out in interviews. For success in 2025, continue practicing coding problems, mock interviews, and stay updated with new Java features.

Comments

Popular posts from this blog

TCS Java Interview | Java | Spring Boot | Microservices | Database

TCS Java Interview | Java | Spring Boot | Microservices | Database This post is based on a Java backend developer interview simulation and provides direct transcript-based answers to help you prepare for similar interviews. 📺 This interview scenario was inspired by a video from the  YouTube channel:  CloudTech . 🔗 Watch the full video here:  https://www.youtube.com/watch?v=yVj2EgwZxk4 1. Can you introduce yourself and talk about your tech stack and domains? I have around 4.5 years of experience working as a Java developer. My core expertise is in Java and developing REST APIs using Spring Boot. I also have basic knowledge of microservices architecture. On the database side, I work with SQL and Oracle. Additionally, I have exposure to UI technologies like Angular, HTML, and CSS, but they are secondary skills. My primary focus is backend Java development. 2. Which version of Java are you currently working on? We are currently using Java 8 but are in the process of upgr...

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers)

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers) * * * 1. What is Microservices Architecture?   → Microservices architecture breaks an application into small, independent services. Each one handles a specific business function and can be developed, deployed, and scaled independently. They usually communicate over REST APIs or messaging queues. * * * 2. How does Spring Boot help in building microservices?   → Spring Boot offers embedded servers, auto-configuration, starter templates, and production-ready tools. It works well with Spring Cloud to provide service discovery, config management, load balancing, and more. * * * 3. What are the core components in a microservices setup?   → Key components include:   ✓ API Gateway   ✓ Eureka (Service Discovery)   ✓ Config Server   ✓ Kafka or RabbitMQ   ✓ Circuit Breaker (like Resilience4j)   ✓ Centralized logging and monitoring (ELK, Prometheus) * * * 4. What i...

Top 15 Spring Boot Interview Questions and Answers – Real Examples (2025)

Top 15 Spring Boot Interview Questions – 2025 Spring Boot is one of the most demanded frameworks for Java backend development. Whether you're interviewing for TCS, Infosys, or a product-based company, these Spring Boot questions will help you prepare like a pro. Here are 15 questions with detailed explanations for developers with 1–2 years of experience. Q1. What is Spring Boot? Answer: Spring Boot is a Java-based open-source framework built on top of the Spring Framework. It helps developers create stand-alone, production-ready Spring applications with minimal configuration. Its key features include: Auto-configuration Embedded servers (Tomcat, Jetty) Starter dependencies Production-ready tools (Actuator, Metrics, etc.) Example: You can create a REST API within minutes by using @RestController and spring-boot-starter-web — no need for external web server deployment. Q2. What is the role of @SpringBootApplication annotation? Answer: This annotation i...