Java OCA OCP Practice Question 1438

Question

Which best describes this code?

class Main { 
   private int data; 
   public int getData() { 
      return data; 
   } 
   public void setData(int data) { 
     this.data = data; 
   } 
} 
  • A. It is a singleton.
  • B. It is well encapsulated.
  • C. It is immutable.
  • D. It is both well encapsulated and immutable.


B.

Note

This code is not a singleton because it has a public constructor.

A public no-argument constructor is provided automatically if no constructor is coded.

This code is well encapsulated because the instance variable is private.

It is not immutable since there is a setter method.

Option B is correct.




PreviousNext

Related