Encapsulation

11.How does encapsulation enhance the flexibility of a Java program?
  • A. By making instance variables static
  • B. By allowing modification of internal logic without affecting external code
  • C. By exposing all methods as public
  • D. By allowing multiple constructors in a class

By allowing modification of internal logic without affecting external code

By allowing modification of internal logic without affecting external code

12.If a class follows encapsulation, how should its fields be accessed?
  • A. Directly using dot notation from another class
  • B. Through getter and setter methods
  • C. By making them static and global
  • D. By using a constructor only

Through getter and setter methods

Through getter and setter methods

13.How does encapsulation improve software reusability?
  • A. By making it easier to modify existing code without breaking dependent modules
  • B. By forcing all variables to be static
  • C. By allowing all methods to be overridden
  • D. By making all variables public

By making it easier to modify existing code without breaking dependent modules

By making it easier to modify existing code without breaking dependent modules

14.Why are setter methods often used with validation checks in encapsulated classes?
  • A. To ensure proper format and prevent invalid values
  • B. To allow unrestricted modification of private variables
  • C. To increase memory usage
  • D. To slow down program execution

To ensure proper format and prevent invalid values

To ensure proper format and prevent invalid values

15.Which of the following violates the principle of encapsulation?
  • A. Declaring instance variables as private
  • B. Providing only necessary public methods to modify data
  • C. Declaring instance variables as public
  • D. Using constructor overloading

Declaring instance variables as public

Declaring instance variables as public