with Complete Solutions
How do you access an object's method in Java?
✔✔ By using the dot `.` operator with the object reference.
What is an instance variable?
✔✔ A variable that belongs to an instance of a class and holds data specific to that object.
How can you make a class member accessible only within the same class?
✔✔ By declaring it as `private`.
What does the `public` access modifier do?
✔✔ It allows a class member to be accessed from anywhere in the program.
What is the purpose of `this` keyword in Java?
✔✔ It refers to the current instance of the class.
1
,Can a class contain multiple constructors?
✔✔ Yes, a class can have multiple constructors with different parameter lists (constructor
overloading).
What happens when you assign one object to another in Java?
✔✔ Both references point to the same object in memory.
What is the purpose of getter and setter methods?
✔✔ Getters retrieve the value of private fields, and setters modify them while enforcing rules.
Why should instance variables be private?
✔✔ To follow the principle of encapsulation and prevent unauthorized access.
What is an instance method?
✔✔ A method that belongs to an object and operates on its instance variables.
Can a class be declared as `final`?
✔✔ Yes, a `final` class cannot be subclassed.
2
, What is a class in Java?
✔✔ A blueprint for creating objects that define their attributes and behaviors.
What is an object in Java?
✔✔ An instance of a class that has its own state and behavior.
How do you define a class in Java?
✔✔ By using the `class` keyword followed by the class name.
What is a constructor in Java?
✔✔ A special method used to initialize objects when they are created.
What happens if a class does not have a constructor?
✔✔ Java provides a default constructor automatically.
What is the purpose of the `new` keyword in Java?
✔✔ It is used to create new objects from a class.
3