Java Freshers Interview Questions and Answers

100 Java Interview Questions and Answers for Freshers
  1. What is Java?

    • Answer: Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers "write once, run anywhere" (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
  2. What is the difference between JDK, JRE, and JVM?

    • Answer: JDK (Java Development Kit) is the full software development kit for Java. It includes the JRE, the compiler (javac), and other tools needed to develop Java applications. JRE (Java Runtime Environment) is the runtime environment needed to run Java applications. It includes the JVM and libraries. JVM (Java Virtual Machine) is the virtual machine that executes Java bytecode. It's platform-specific and translates bytecode into machine code.
  3. What are the basic data types in Java?

    • Answer: The basic data types in Java are byte, short, int, long, float, double, boolean, and char. These represent different sizes and ranges of numerical and boolean values.
  4. What is an object?

    • Answer: An object is an instance of a class. It's a concrete entity that has state (data) and behavior (methods) defined by its class.
  5. What is a class?

    • Answer: A class is a blueprint for creating objects. It defines the state (data – variables) and behavior (actions – methods) of objects.
  6. Explain the concept of inheritance.

    • Answer: Inheritance is a mechanism where one class (subclass or derived class) acquires the properties and behaviors of another class (superclass or base class). It promotes code reusability and establishes a hierarchical relationship between classes.
  7. What are the different types of inheritance in Java?

    • Answer: Java supports single inheritance (a class extending only one other class) and multiple inheritance through interfaces (a class implementing multiple interfaces).
  8. What is polymorphism?

    • Answer: Polymorphism means "many forms." In Java, it allows objects of different classes to be treated as objects of a common type. This is achieved through method overriding and method overloading.
  9. What is method overloading?

    • Answer: Method overloading is the ability to have multiple methods with the same name but different parameters (number, type, or order) within the same class.
  10. What is method overriding?

    • Answer: Method overriding is when a subclass provides a specific implementation for a method that is already defined in its superclass. The method signature (name and parameters) must be the same.
  11. What is an interface?

    • Answer: An interface is a reference type similar to a class, but it can only contain constants, method signatures, default methods, static methods, and nested types. A class can implement multiple interfaces.
  12. What is an abstract class?

    • Answer: An abstract class is a class that cannot be instantiated directly. It can contain both abstract methods (methods without a body) and concrete methods (methods with a body). It serves as a blueprint for subclasses.
  13. What is encapsulation?

    • Answer: Encapsulation is the bundling of data (variables) and methods that operate on that data within a single unit (class). It protects data by hiding internal implementation details and exposing only necessary interfaces.
  14. What is the difference between static and non-static methods?

    • Answer: Static methods belong to the class itself, not to any specific object of the class. They can be called directly using the class name. Non-static methods (instance methods) belong to objects of the class and must be called on an object.
  15. What is a constructor?

    • Answer: A constructor is a special method within a class that is automatically called when an object of the class is created. It initializes the object's state.
  16. What is the difference between `==` and `.equals()`?

    • Answer: `==` compares object references (memory addresses). `.equals()` compares the content of objects. The default implementation of `.equals()` is the same as `==`, but it can be overridden to provide custom comparison logic.
  17. What is the `final` keyword?

    • Answer: The `final` keyword can be used with variables, methods, and classes. For variables, it means the value cannot be changed after initialization. For methods, it means the method cannot be overridden in subclasses. For classes, it means the class cannot be extended.
  18. What is an exception?

    • Answer: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. They are handled using `try-catch` blocks.
  19. Explain the `try-catch` block.

    • Answer: The `try-catch` block is used to handle exceptions. The code that might throw an exception is placed in the `try` block. If an exception occurs, the corresponding `catch` block is executed.
  20. What is a `finally` block?

    • Answer: The `finally` block is used to define code that always executes, regardless of whether an exception occurred or not. It's often used for cleanup tasks (e.g., closing files).
  21. What is a checked exception?

    • Answer: A checked exception is an exception that the compiler forces you to handle (either by using a `try-catch` block or declaring it in the method signature using `throws`).
  22. What is an unchecked exception?

    • Answer: An unchecked exception is an exception that the compiler does not force you to handle. These are typically runtime exceptions (e.g., `NullPointerException`, `ArrayIndexOutOfBoundsException`).
  23. What is the difference between `throw` and `throws`?

    • Answer: `throw` is used to explicitly throw an exception. `throws` is used in a method signature to declare that the method might throw one or more checked exceptions.
  24. What is a String in Java?

    • Answer: A String is an immutable sequence of characters. It's a fundamental data type used for representing text.
  25. What is the difference between String and StringBuilder?

    • Answer: String is immutable; every modification creates a new String object. StringBuilder is mutable; modifications are done in place, making it more efficient for frequent string manipulations.
  26. What is an array?

    • Answer: An array is a data structure that stores a fixed-size sequence of elements of the same data type.
  27. What is a collection?

    • Answer: A collection is a framework that provides ways to store and manipulate groups of objects. It offers various data structures like Lists, Sets, and Maps.
  28. What are some common collection types in Java?

    • Answer: Common collection types include ArrayList, LinkedList, HashSet, TreeSet, HashMap, TreeMap.
  29. What is the difference between ArrayList and LinkedList?

    • Answer: ArrayList uses an array internally, providing fast random access (getting an element by index) but slower insertions and deletions. LinkedList uses a doubly linked list, providing fast insertions and deletions but slower random access.
  30. What is a HashSet?

    • Answer: A HashSet is a collection that stores unique elements in no particular order. It uses a hash table for fast lookups.
  31. What is a HashMap?

    • Answer: A HashMap is a collection that stores key-value pairs. It uses a hash table for fast lookups based on the key.
  32. What is the difference between HashMap and TreeMap?

    • Answer: HashMap stores key-value pairs in no particular order. TreeMap stores key-value pairs in sorted order based on the key.
  33. What is an iterator?

    • Answer: An iterator is an object that allows you to traverse through a collection. It provides methods like `hasNext()` and `next()` to iterate over elements.
  34. What is a ListIterator?

    • Answer: A ListIterator is a more powerful iterator specifically designed for Lists. It allows bidirectional traversal (moving forward and backward) and modification of the list during iteration.
  35. What is generics?

    • Answer: Generics allow you to write type-safe code by parameterizing types. This helps avoid runtime type errors and improves code readability.
  36. Explain the concept of autoboxing and unboxing.

    • Answer: Autoboxing is the automatic conversion of primitive types (like `int`) to their corresponding wrapper classes (like `Integer`). Unboxing is the reverse process.
  37. What is a wrapper class?

    • Answer: Wrapper classes are classes that provide object representations of primitive data types (e.g., `Integer`, `Double`, `Boolean`).
  38. What are access modifiers in Java?

    • Answer: Access modifiers control the visibility and accessibility of classes, methods, and variables. They include `public`, `private`, `protected`, and default (package-private).
  39. What is the difference between `public`, `private`, and `protected` access modifiers?

    • Answer: `public` members are accessible from anywhere. `private` members are only accessible within the same class. `protected` members are accessible within the same class, subclasses, and packages.
  40. What is a package in Java?

    • Answer: A package is a way to organize classes and interfaces into namespaces. It helps avoid naming conflicts and improves code organization.
  41. How do you import a package in Java?

    • Answer: You import a package using the `import` keyword followed by the package name (e.g., `import java.util.ArrayList;`).
  42. What is a static block?

    • Answer: A static block is a block of code that is executed only once when the class is loaded. It's often used for initializing static variables.
  43. What is a Singleton class?

    • Answer: A Singleton class is a class that ensures only one instance of itself is created. It's often used for managing resources or configurations.
  44. What is JVM (Java Virtual Machine)?

    • Answer: The Java Virtual Machine (JVM) is an abstract machine. It's a runtime environment that enables a computer to run Java programs as well as programs in other languages that are also compiled to Java bytecode.
  45. What is JIT (Just-In-Time) compilation?

    • Answer: JIT compilation is a technique used by JVMs to improve the performance of Java programs. It compiles Java bytecode into native machine code at runtime, optimizing execution.
  46. What is garbage collection in Java?

    • Answer: Garbage collection is the automatic process of reclaiming memory occupied by objects that are no longer referenced by the program. It prevents memory leaks.
  47. What is the difference between `System.out.println()` and `System.out.print()`?

    • Answer: `System.out.println()` prints the output and moves the cursor to the next line. `System.out.print()` prints the output without moving the cursor to the next line.
  48. What is a multithreaded program?

    • Answer: A multithreaded program is a program that can execute multiple threads concurrently. Threads are independent units of execution within a program.
  49. How do you create a thread in Java?

    • Answer: You can create a thread by extending the `Thread` class or implementing the `Runnable` interface.
  50. What is the difference between the `run()` and `start()` methods of a thread?

    • Answer: `run()` simply executes the thread's code in the current thread. `start()` creates and starts a new thread that executes the `run()` method.
  51. What is thread synchronization?

    • Answer: Thread synchronization is a mechanism to control access to shared resources among multiple threads to prevent race conditions and ensure data consistency.
  52. What is a deadlock?

    • Answer: A deadlock is a situation where two or more threads are blocked indefinitely, waiting for each other to release the resources that they need.
  53. What is a race condition?

    • Answer: A race condition occurs when multiple threads access and manipulate shared resources concurrently, leading to unpredictable results.
  54. What is the purpose of the `synchronized` keyword?

    • Answer: The `synchronized` keyword is used to provide mutual exclusion, ensuring that only one thread can access a shared resource (method or block of code) at a time.
  55. What is a semaphore?

    • Answer: A semaphore is a synchronization primitive that controls access to a shared resource by multiple processes or threads. It maintains a counter that represents the number of available resources.
  56. What is a mutex?

    • Answer: A mutex (mutual exclusion) is a locking mechanism that ensures only one thread can access a shared resource at a time. It's often used for protecting critical sections of code.
  57. What is an immutable class?

    • Answer: An immutable class is a class whose objects cannot be modified after creation. The String class is a prime example.
  58. What is the difference between `==` and `.equals()` when comparing Strings?

    • Answer: `==` compares the memory addresses of the String objects. `.equals()` compares the content of the String objects.
  59. What is a design pattern?

    • Answer: A design pattern is a reusable solution to a commonly occurring problem within a specific context in software design.
  60. Name a few common design patterns.

    • Answer: Some common design patterns include Singleton, Factory, Observer, Decorator, and Strategy.
  61. What is an ArrayList?

    • Answer: An ArrayList is a dynamic array implementation that allows you to add and remove elements. It's part of the Java Collections Framework.
  62. What is a LinkedList?

    • Answer: A LinkedList is a doubly linked list implementation. It's efficient for insertions and deletions but slower for random access.
  63. What is a HashSet?

    • Answer: A HashSet is a set implementation that does not allow duplicate elements. It uses a hash table for fast lookups.
  64. What is a HashMap?

    • Answer: A HashMap is a map implementation that stores key-value pairs. It uses a hash table for fast lookups based on the key.
  65. Explain the concept of object serialization.

    • Answer: Object serialization is the process of converting an object into a byte stream so it can be stored in a file or transmitted over a network. It allows you to persist object state.
  66. What is object deserialization?

    • Answer: Object deserialization is the reverse process of object serialization. It reconstructs an object from a byte stream.
  67. What is the purpose of the `transient` keyword?

    • Answer: The `transient` keyword indicates that a member variable of a class should not be serialized when the object is serialized.
  68. What is JDBC?

    • Answer: JDBC (Java Database Connectivity) is an API for connecting Java applications to relational databases.
  69. What are the steps involved in connecting to a database using JDBC?

    • Answer: The steps typically involve loading the JDBC driver, establishing a connection, creating a statement object, executing queries, processing results, and closing the connection.
  70. What is a PreparedStatement?

    • Answer: A PreparedStatement is a pre-compiled SQL statement that can be executed multiple times with different parameters, improving performance and security.
  71. What is a ResultSet?

    • Answer: A ResultSet is a table of data representing the result of a database query. It provides methods for navigating and accessing the data.
  72. What is I/O in Java?

    • Answer: I/O (Input/Output) refers to the process of reading data from and writing data to external sources, such as files, networks, or the console.
  73. What are streams in Java?

    • Answer: Streams are a powerful feature introduced in Java 8 that provide a declarative way to process collections of data. They support functional-style operations like filtering, mapping, and reducing.
  74. What is Lambda expression?

    • Answer: A lambda expression is a concise way to represent anonymous functions in Java. They are often used with streams and functional interfaces.
  75. What is a functional interface?

    • Answer: A functional interface is an interface that has exactly one abstract method. It can have multiple default methods or static methods.
  76. What is method reference in Java?

    • Answer: A method reference is a concise way to refer to an existing method without explicitly creating a lambda expression. It uses the `::` operator.
  77. What is JavaFX?

    • Answer: JavaFX is a set of graphics and media packages that allows developers to design and develop rich client applications. It's used for creating desktop GUI applications.
  78. What is Spring Framework?

    • Answer: The Spring Framework is a popular Java application framework that simplifies application development by providing features like dependency injection, aspect-oriented programming, and transaction management.
  79. What is dependency injection?

    • Answer: Dependency injection is a design pattern where dependencies are provided to a class instead of being created within the class. This promotes loose coupling and testability.
  80. What is aspect-oriented programming (AOP)?

    • Answer: Aspect-oriented programming (AOP) is a programming paradigm that separates cross-cutting concerns (like logging or security) from the core business logic. It enhances modularity and maintainability.
  81. What is REST?

    • Answer: REST (Representational State Transfer) is an architectural style for designing networked applications. It uses standard HTTP methods (GET, POST, PUT, DELETE) to interact with resources.
  82. What is a servlet?

    • Answer: A servlet is a Java program that runs on a server and extends the capabilities of a server. Servlets are commonly used for creating web applications.
  83. What is JSP?

    • Answer: JSP (JavaServer Pages) is a technology that allows you to embed Java code within HTML pages to generate dynamic web content.
  84. What is Hibernate?

    • Answer: Hibernate is an object-relational mapping (ORM) framework for Java that simplifies database interactions by mapping Java objects to database tables.
  85. What is an object-relational mapping (ORM)?

    • Answer: An object-relational mapping (ORM) is a programming technique that maps data between incompatible type systems, in this case, relational databases and object-oriented programming languages.
  86. What is Maven?

    • Answer: Maven is a build automation tool for Java projects. It manages dependencies, compiles code, runs tests, and packages applications.
  87. What is Gradle?

    • Answer: Gradle is another popular build automation tool for Java projects. It's known for its flexibility and performance.
  88. What is Git?

    • Answer: Git is a distributed version control system that tracks changes to files and allows developers to collaborate on projects efficiently.

Thank you for reading our blog post on 'Java Freshers Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!