Java Module 2 – Important Questions and Answers 1. Define a class and an object.
A class is a blueprint for objects that defines data and methods. An object is an instance of a class
created using the new keyword.
Example:
Car myCar = new Car();
2. What is a constructor?
A constructor initializes objects. It has the same name as the class and no return type.
3. Explain the use of the this keyword.
The this keyword refers to the current object. It is used to access class variables and methods.
4. What is garbage collection in Java?
Garbage Collection automatically removes unused objects to free memory.
5. Difference between declaring and creating an object.
Declaring defines the reference; creating allocates memory using new.
Example:
Car c; (declaration)
c = new Car(); (creation)
6. What is method overloading?
Having multiple methods with the same name but different parameter lists.
7. Explain how objects are passed as parameters.
Objects are passed by reference value; changes inside a method affect the original object.
8. What is the purpose of static keyword?
Static members belong to the class, not instances. Shared among all objects.
9. Difference between final variable, method, and class.
Final variable cannot be changed, final method cannot be overridden, final class cannot be
inherited.
10. What are nested and inner classes?
Nested class is a class within another. Inner class (non-static) can access outer class members.