Encapsulation

1.What is the primary goal of encapsulation in Java?
  • A. To allow multiple inheritance
  • B. To restrict direct access to data and protect object integrity
  • C. To increase code redundancy
  • D. To allow global variables across classes

To restrict direct access to data and protect object integrity

To restrict direct access to data and protect object integrity

2.How does encapsulation contribute to the maintainability of code?
  • A. By allowing external classes to modify object data directly
  • B. By enforcing modular code design and reducing dependencies
  • C. By eliminating the need for constructors
  • D. By making all variables globally accessible

By enforcing modular code design and reducing dependencies

By enforcing modular code design and reducing dependencies

3.Which Java feature enforces encapsulation by restricting direct access to object data?
  • A. Static methods
  • B. Instance variables with public access
  • C. Getters and setters
  • D. Global variables

Getters and setters

Getters and setters

4.What is a common consequence of not using encapsulation?
  • A. Increased security of data
  • B. Unrestricted modification of object properties
  • C. Prevention of method overriding
  • D. Automatic garbage collection

Unrestricted modification of object properties

Unrestricted modification of object properties

5.Why should instance variables typically be declared as private?
  • A. To prevent unauthorized access and maintain data integrity
  • B. To allow direct modification from outside the class
  • C. To improve execution speed
  • D. To make variables accessible to all other classes

To prevent unauthorized access and maintain data integrity

To prevent unauthorized access and maintain data integrity