1. What is the difference between an object and a class in Java?
An object is an instance of a class, while a class is a blueprint or template for
creating objects.
2. What is the purpose of the main method in a Java program?
The main method is the entry point of a Java program and is where the program's
execution begins. It is also the method that is called when the program is run.
3. How does Java handle memory management?
Java uses a garbage collector to automatically manage memory by removing
unreferenced objects and freeing up memory.
4. What is the difference between a static method and a non-static method in
Java?
A static method belongs to the class and can be called without creating an instance
of the class, while a non-static method belongs to an object and can only be called
on an instance of the class.
5. What is an interface in Java?
An interface in Java is a collection of abstract methods that a class must implement.
It is used to define a contract for a class to follow, without specifying the
implementation details.
6. What is the difference between a String and a StringBuilder in Java?
, A String is an immutable object, which means its value cannot be changed once it is
created. A StringBuilder, on the other hand, is a mutable object, which means its
value can be modified. StringBuilder is generally used when many operations are
performed on a String.
7. How can we achieve polymorphism in Java?
Polymorphism can be achieved in Java by using method overriding and/or method
overloading.
8. What is the difference between a constructor and a method in Java?
A constructor is a special method used to initialize the state of an object when it is
created, while a method is a function that can be called on an object to perform a
specific action.
9. What is an exception in Java?
An exception in Java is an event that occurs during the execution of a program that
disrupts the normal flow of instructions. Exceptions are used to handle errors and
other abnormal conditions in a program.
10. How can we handle exceptions in Java?
Exceptions can be handled in Java using try-catch blocks. The code that might throw
an exception is placed in a try block, and the code that handles the exception is
placed in a catch block.
11. What is the difference between an Array and an ArrayList in Java?
An Array is a fixed-size collection of elements, while an ArrayList is a resizable array
implementation of the List interface. ArrayList can grow or shrink in size dynamically.