java Interview Questions
java Interview Questions
**
Java is a high-level, object-oriented programming language that is platform-
independent. It follows the principle of "write once, run anywhere," meaning
compiled Java code can run on all platforms that support Java without needing
recompilation.
19. **What is the difference between method overloading and method overriding?**
- **Method Overloading**: Same method name with different parameter lists in
the same class (compile-time polymorphism).
- **Method Overriding**: Subclass provides a specific implementation of a
method already defined in the superclass (runtime polymorphism).
22. **What is the difference between a static and non-static method in Java?**
- **Static Method**: Belongs to the class, not the instance. It can be called
without creating an instance of the class.
- **Non-static Method**: Belongs to an object of the class. It can access
instance variables and methods.
23. **What is the final keyword in Java?**
The `final` keyword in Java is used to restrict the user. It can be applied to
variables (to make them constants), methods (to prevent overriding), and classes
(to prevent inheritance).
in Java?**
The `try-catch` block is used to handle exceptions. Code that may throw an
exception is placed inside the `try` block, and exception handling code is placed
in the `catch` block.
53. **What are the steps to connect to a database in Java using JDBC?**
Steps to connect to a database using JDBC:
- Load the JDBC driver.
- Establish a connection using `DriverManager.getConnection()`.
- Create a `Statement` or `PreparedStatement` object to execute queries.
- Execute the query and process the results.
- Close the connection.
56. **What is the difference between doGet() and doPost() methods in Servlets?**
- **doGet()**: Handles HTTP GET requests. Used for requests where the data is
appended to the URL.
- **doPost()**: Handles HTTP POST requests. Used for requests where data is
sent in the request body.
74. **What is the difference between @Override and @Deprecated annotations in Java?
**
- `@Override`: Indicates a method is overriding a method in the superclass.
- `@Deprecated`: Marks a method, class, or field as obsolete and indicates that
it should not be used.
75. **What is the difference between the final, finally, and finalize keywords in
Java?**
- `final`: Used to declare constants, prevent method overriding, or prevent
inheritance.
- `finally`: Block used for code that will always