Java Interview Questions
By,
Aavanto Pvt. Ltd.

Start your Abroad Journey!

Call: +91- 8639005305

Know More

arrow
sitting place

1. What is Java?

Java is a widely-used programming language that is platform-independent, meaning it can run on different operating systems. It is known for its simplicity, object-oriented nature, and robustness.

2. What is the difference between JDK and JRE?

JDK stands for Java Development Kit, which includes tools necessary for developing and running Java programs. JRE stands for Java Runtime Environment, which is required to run Java applications.

3. Explain the concept of object-oriented programming (OOP).

OOP is a programming paradigm that organizes code around objects, which are instances of classes. It promotes concepts like encapsulation, inheritance, and polymorphism, making code modular, reusable, and easier to maintain.

4. What is a class in Java?

A class in Java is a blueprint or template that defines the structure and behavior of objects. It encapsulates data (in the form of fields) and operations (in the form of methods) that can be performed on the objects.

5. What is the difference between an object and a class?

A class is a blueprint or template, whereas an object is an instance of a class. In other words, a class defines the properties and behaviors of objects, while an object represents a specific instance of that class.

6. What is the purpose of the "static" keyword?

The "static" keyword in Java is used to declare members (variables and methods) that belong to the class itself, rather than specific instances of the class. These members can be accessed using the class name, without creating an object.

7. Explain the concept of inheritance.

Inheritance is a feature in Java that allows a class (called the child or subclass) to inherit the properties and methods of another class (called the parent or superclass). It promotes code reusability and establishes an "is-a" relationship between classes.

8. What is the difference between inheritance and composition?

Inheritance involves deriving a class from another class to inherit its properties and methods. Composition, on the other hand, involves creating objects of other classes within a class to achieve reusability and establish a "has-a" relationship.

9. What is method overriding?

Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. It allows the subclass to define a behavior that is specific to itself, while still retaining the same method signature.

10. What is the purpose of the "final" keyword?

The "final" keyword in Java can be applied to variables, methods, and classes. When applied to a variable, it makes the variable a constant and prevents it from being modified. When applied to a method, it prevents the method from being overridden. When applied to a class, it prevents the class from being subclassed.

11. What is an interface?

An interface in Java is a collection of abstract methods, which define a contract that implementing classes must adhere to. It allows for multiple inheritance of behavior and promotes loose coupling between classes.

12. What is the difference between an abstract class and an interface?

An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods. An abstract class can provide default implementations for some or all of its methods, while an interface cannot. A class can implement multiple interfaces, but it can only inherit from one abstract class.

13. Explain the concept of method overloading.

Method overloading is a feature in Java that allows multiple methods with the same name but different parameters to exist in the same class. It enables the same method name to be used for different behaviors based on the type and number of arguments.

14. What is the difference between checked and unchecked exceptions?

Checked exceptions are exceptions that must be declared in a method's signature using the "throws" keyword or caught using a try-catch block. Unchecked exceptions, on the other hand, do not need to be declared or caught explicitly.

15. What is a constructor?

A constructor is a special method in Java that is used to initialize objects of a class. It has the same name as the class and is called automatically when an object is created. Constructors can be used to set initial values to object properties or perform other initialization tasks.

16. What is the purpose of the "this" keyword?

The "this" keyword in Java is a reference to the current object. It is often used to refer to instance variables or methods of the current object, especially when there is a naming conflict with local variables or parameters.

17. What is method chaining?

Method chaining is a coding style in which multiple methods are called on the same object in a single line of code. It is achieved by returning the current object from each method, allowing subsequent method calls to be chained together.

18. What is the difference between String, StringBuilder, and StringBuffer?

String is an immutable class, meaning its value cannot be changed once it is created. StringBuilder and StringBuffer, on the other hand, are mutable classes that allow for efficient manipulation of strings. StringBuffer is synchronized and thread-safe, while StringBuilder is not.

19. What is the purpose of the "equals" method?

The "equals" method in Java is used to compare the equality of objects. It is typically overridden in classes to provide a custom definition of equality based on the object's properties, rather than the default reference equality.

20. What is the difference between "==" and the "equals" method?

The "==" operator in Java is used to compare the reference equality of objects, meaning it checks if two object references point to the same memory location. The "equals" method, on the other hand, is used to compare the logical equality of objects, based on the implementation provided by the class.

21. What is the purpose of the "hashCode" method?

The "hashCode" method in Java is used to generate a unique hash code for an object. It is often used in data structures like hash tables to efficiently store and retrieve objects. The "hashCode" method should be overridden in classes that override the "equals" method.

22. What is the difference between the stack and the heap?

In Java, the stack is used to store method calls, local variables, and references to objects. It is a region of memory that is automatically managed by the JVM. The heap, on the other hand, is used to dynamically allocate memory for objects and their data. It is a larger region of memory that is also managed by the JVM.

23. Explain the concept of garbage collection.

Garbage collection is a process in Java where the JVM automatically reclaims memory occupied by objects that are no longer reachable or in use by the program. It helps in managing memory efficiently and eliminates the need for manual memory deallocation.

24. What is the purpose of the "finalize" method?

The "finalize" method in Java is a special method that is called by the JVM before an object is garbage collected. It can be overridden in a class to perform cleanup operations or release resources held by the object.

25. What are generics in Java?

Generics allow the use of type parameters in classes, interfaces, and methods to create reusable code that can work with different types. They provide compile-time type safety and eliminate the need for explicit type casting.

26. What is the difference between an ArrayList and a LinkedList?

An ArrayList is implemented as a dynamically resizing array, providing fast access to elements by index. A LinkedList, on the other hand, is implemented as a doubly-linked list, providing efficient insertion and deletion of elements at both ends of the list. ArrayList is generally preferred for random access, while LinkedList is preferred for frequent modifications.

27. What is the purpose of the "synchronized" keyword?

The "synchronized" keyword in Java is used to achieve thread safety by ensuring that only one thread can access a synchronized block or method at a time. It prevents data inconsistencies and race conditions in multi-threaded environments.

28. What is the difference between the "wait" and "sleep" methods?

The "wait" method is used for inter-thread communication, allowing a thread to wait until it is notified by another thread. It is typically used in conjunction with the "notify" or "notifyAll" methods. The "sleep" method, on the other hand, is used to pause the execution of a thread for a specified period of time.

29. What is the purpose of the "volatile" keyword?

The "volatile" keyword in Java is used to declare variables that can be accessed by multiple threads. It ensures that changes made to a volatile variable are immediately visible to all threads, preventing issues like stale data or thread interference.

30. What is the difference between a thread and a process?

A thread is a lightweight unit of execution within a process. Multiple threads can exist within a single process and share the same memory space, allowing for concurrent execution. A process, on the other hand, is an instance of a program that is executed independently and has its own memory space.

31. What is the purpose of the "java.lang" package?

The "java.lang" package in Java contains fundamental classes and interfaces that are automatically imported into every Java program. It includes classes like Object, String, Math, and exceptions like NullPointerException and IllegalArgumentException.

32. Explain the concept of an exception.

An exception in Java is an event that occurs during the execution of a program and disrupts the normal flow of instructions. It can be caused by various factors like invalid input, resource unavailability, or programming errors. Exceptions are categorized into checked and unchecked exceptions.

33. What is the difference between a checked exception and an unchecked exception?

A checked exception is a type of exception that must be declared or caught explicitly, either by using the "throws" clause or by enclosing the code in a try-catch block. Unchecked exceptions, on the other hand, do not need to be declared or caught explicitly.

34. What is the purpose of the "try-catch-finally" block?

The "try-catch-finally" block in Java is used to handle exceptions. Code that might throw an exception is enclosed within the "try" block, and any exceptions that are thrown are caught and handled in the "catch" block. The "finally" block is optional and is used to specify cleanup code that should be executed regardless of whether an exception occurs or not.

35. What is the purpose of the "throw" keyword?

The "throw" keyword in Java is used to explicitly throw an exception. It is typically used within a method to indicate exceptional situations or errors. When an exception is thrown, the control is transferred to the nearest enclosing "try-catch" block that can handle it.

36. What is the purpose of the "throws" keyword?

The "throws" keyword in Java is used to declare that a method may throw a checked exception. It is used in the method signature to inform the caller that they should handle the exception or propagate it further.

37. What are the access modifiers in Java?

Java provides four access modifiers: "public," "private," "protected," and the default (no modifier). "public" allows access from any class, "private" restricts access to within the same class, "protected" allows access within the same class, subclasses, and the same package, and the default allows access within the same package.

38. What is the purpose of the "super" keyword?

The "super" keyword in Java is used to refer to the superclass of a class. It can be used to call the superclass's constructor, access its members, or invoke its overridden methods. It is often used to differentiate between superclass and subclass members that have the same name.

39. What is method visibility or method hiding in Java?

Method visibility, also known as method hiding, occurs when a subclass defines a static method with the same signature as a static method in its superclass. The subclass's method hides the superclass's method, and the choice of which method to invoke is determined at compile-time based on the type of the reference.

40. What is the purpose of the "instanceof" operator?

The "instanceof" operator in Java is used to check if an object is an instance of a particular class or implements a particular interface. It returns a boolean value indicating whether the object is of the specified type or a subtype.

41. What is the purpose of the "transient" keyword?

The "transient" keyword in Java is used to indicate that a variable should not be serialized when an object is serialized. It is commonly used for variables that do not need to be persisted or for variables that contain sensitive information.

42. What is the purpose of the "static" initializer block?

The "static" initializer block is a block of code that is executed when the class is loaded by the JVM. It is used to initialize static variables or perform any other one-time initialization tasks for the class.

43. What is the difference between a shallow copy and a deep copy?

A shallow copy creates a new object that shares the same memory as the original object, while a deep copy creates a new object with its own memory and copies the values of the original object's fields. In a shallow copy, changes to the copied object may affect the original object, while in a deep copy, the two objects are completely independent.

44. What is the purpose of the "assert" keyword?

The "assert" keyword in Java is used to assert the truth of an expression during program execution. It is primarily used for testing purposes and can help in identifying logical errors or incorrect assumptions in the code.

45. What are annotations in Java?

Annotations in Java provide metadata about program elements like classes, methods, or variables. They can be used for documentation, code generation, or runtime behavior modification. Annotations are defined using the "@" symbol and can be custom-defined or predefined.

46. What is the purpose of the "Enum" class in Java?

The "Enum" class in Java is used to define enumerated types, which represent a fixed set of values. Enumerations provide type safety, and the values are typically used as constants. Enum classes can have their own methods, constructors, and fields.

47. What is the purpose of the "Lambda" expression in Java?

A Lambda expression in Java is a concise way to represent an anonymous function or a single-method interface. It simplifies the syntax of functional programming by allowing the definition of a function inline, without the need for a separate class or method.

48. What is the difference between checked and unchecked exceptions in Java?

Checked exceptions are exceptions that must be declared in a method's signature using the "throws" keyword or caught using a try-catch block. Unchecked exceptions, on the other hand, do not need to be declared or caught explicitly.

49. What is the purpose of the "Optional" class in Java?

The "Optional" class in Java is used to represent an object that may or may not exist. It provides a way to handle null values more explicitly and avoids potential null pointer exceptions. It encourages developers to explicitly handle the absence of a value.

50. What is the purpose of the "var" keyword in Java?

The "var" keyword in Java is used to declare local variables with inferred types. It allows for more concise code and reduces the need for explicitly specifying the type. The compiler infers the type based on the initializer expression.