11. What is a Java constructor, and how does it differ from a method?

  • A constructor is a special method that is called when an object is created. It has the same name as the class and doesn’t have a return type. Its purpose is to initialize object state.

12.  Explain the purpose of the super keyword in Java.

  • The super keyword is used to access and call a superclass’s members, including methods and constructors, from a subclass.

13. What is the try-catch block in Java, and how does it handle exceptions?

  • A try-catch block is used to handle exceptions in Java. Code within the try block is executed, and if an exception occurs, the catch block is executed to handle the exception.

14. Describe the difference between the Runnable and Thread interfaces in Java for implementing multithreading.

  • The Runnable interface is preferred for implementing multithreading because it separates the task from the thread itself, promoting better code organization and reuse. In contrast, the Thread class directly extends the thread behavior.

15.  What is the purpose of the this keyword in Java?

  • The this keyword is used to refer to the current instance of the class. It’s often used to disambiguate instance variables from method parameters or to call one constructor from another in the same class.

16.  Explain the concept of method overloading in Java.

  • Method overloading allows multiple methods in the same class to have the same name but different parameter lists. The method to be called is determined at compile time based on the arguments passed.

17.  What is the difference between HashMap and HashTable in Java?

  • HashMap is not synchronized and allows null values, while HashTable is synchronized and does not allow null keys or values.

18. What is the purpose of the transient keyword in Java?

  •  The transient keyword is used to indicate that a field should not be serialized when an object is converted to a byte stream.

19. Explain the concept of method overriding in Java.

  • Method overriding allows a subclass to provide a specific implementation of a method defined in its superclass. The overridden method in the subclass has the same name, return type, and parameters.

20. What are the advantages of using the StringBuilder class over String for string manipulation?

  • StringBuilder is more efficient for string manipulation because it allows in-place modification of strings without creating new string objects, leading to better performance